Linux based text operation

Posted by nishmgopal on Mon, 31 Jan 2022 20:46:42 +0100

1 file stream

There are three kinds of input / output streams in Linux shell: standard input stream, standard output stream and standard error stream. Each stream has a file descriptor, a commonly used abbreviated name, and a commonly used default device (this is interesting).
Keyboard input (the default device) is actually sending input to the standard output stream, which is abbreviated as stdin and identified as 0.
The terminal display (default device) is actually the standard output stream, which is abbreviated as stdout and identified as 1.
The error message (default device) displayed on the terminal is actually the standard error flow, which is abbreviated as stderr and identified as 2.
[the above three identifications can be used to judge ($?) by using the last output signal after a command is executed Whether the command \ script execution is successful, if $? 2 means execution failed.]

2 the most commonly used logical operators

Scenario 1: there are currently two commands. If you want to finish the previous one, then execute the next one;

&&Symbolic link. If this symbolic link is used, the next one will be executed only when the previous one is executed successfully.

Scenario 2: for the current two commands, if the previous one fails, the latter one will be executed; (exit if failed)

||When the previous one fails, the latter one (clear. Sh | return) will be executed. If the execution fails, the system will exit.

3. Use stdin and stdout for the stream

Scenario: commands, script execution process and results need to be output to text and window at the same time.

Generally, the background or > > will output the results and processes to the specified file. At this point, use the tee command.

tee will split the output into two streams, one to stdout and one to stdin

#!/bin/bash
ls -la | tee test.txt
# At this time, the window will output and there will be input in the file

4 file viewing

In most cases, you can see the file type according to the file suffix, but in Linux, the file suffix (extension) is not a necessary part of using the file to work normally. Their existence is just a habit, which is the residual impact of other operating systems.

Generally, ls -lF is used, but only general information such as files, folders and executables can be displayed, and the actual type is still unknown. The file command can view the file type and file attributes

file file_name ;
file * # View all file types in the current folder
#Alive. 1080p HD Chinese character: directory
[www.66ys.org]Law abiding citizen BD Chinese and English subtitles 1024 x576 Anhydrous printing plate.rmvb:         RealMedia file
0C844F52E8350E67FD41BC45AFCBC16EA7B88830.torrent:              BitTorrent file
43B9C14A60C2DDA9BB55B9FC8A7A0779B18B69CE.torrent:              BitTorrent file
800:                                                           directory
B0444518E3B2FB1933C769F44D4986031BCC0659.torrent:              BitTorrent file
B4263935317AD1103550783132B57456CC3A854B.torrent:              BitTorrent file
D50319A98262D564E2E3B2E05866D72933868219.torrent:              BitTorrent file
Everything-1.4.1.988.x86-Setup.exe:                            PE32 executable (GUI) Intel 80386, for MS Windows
hat.1080p.BD Chinese and English double characters[The latest movie www.6vhao.tv].mp4:                ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]
teleport-assist-windows-3.0.1.6.exe:                           PE32 executable (GUI) Intel 80386, for MS Windows, Nullsoft Installer self-extracting archive
 The fantasy adventure of Artemis.1080p.HD Chinese and English double characters[66 Movies www.66Ys.Co].mp4:    ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]
Ava.1080p.HD Chinese and English double characters:                                         directory
 Love and monster.1080p.BD Chinese and English double characters:                                     directory
 Miserable world.1080p.HD Chinese characters[66 Movies www.66Ys.Co].mp4:                  ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]
Superman: Son of tomorrow.1080p.HD Chinese characters:                                   directory
templates:                                               directory
test.py:                                                 Python script, UTF-8 Unicode text executable, with CRLF line terminators
tornado-resources:                                       directory
tornado-resources-master.zip:                            Zip archive data, at least v1.0 to extract
untitled:                                                directory
uwsgi.ini:                                               UTF-8 Unicode text
 test.py:                                                 Python script, UTF-8 Unicode text executable, with CRLF line terminators
 test.py:                                                 Python script, UTF-8 Unicode text executable, with CRLF line terminators

5 file splicing

Requirements combine two files into one file

cat file1 file2 > file3 

At this time, stdout is input to stdin. At this time, if cat file1 File2 > File2 is used, an error will be reported. Files cannot be input and output at the same time.

6. Output the line number when displaying the file,

 cat -n

7 Step through the text of the file.

Using cat can make it easy to see files, but if the log file is too large, it is completely useless.

More, less, pg and most are all OK, but less is an improved version of more released in 1985. Less is really better than
more easy to use

less the most used page turning shortcut

pageDe,e Or space  Forward one page
PageUp ,b      Back one page
e,j,return,The down arrow advances one line
y,k,up arrow     Back one line
G p             Advance to end of file
1G              Go back to the beginning of the file
Q          sign out less command

After using less, then use = (equal sign) in the interface to view the file information, the current number of pages, etc.

less -N file can display the number of rows when viewing,

Less also has a less used way to view and then edit the current page. After using less and then v, the current page will be edited, and then the view will continue after exiting. Of course, we still use vim most, so this command is a bit weak.

8. tail and head commands

tail view the number of lines at the end of the file. The default is 10 lines,

tail -100f , directly see the last 100 lines and continuously output to the window display. This command can also view multiple files at the same time.

head view the first 10 lines of the file,

head -n 5 view the first 5 lines of a file, followed by multiple files.

 

 

Topics: Linux Operation & Maintenance CentOS less