9.4 sed (UP)
Sed is a stream editor, which is a very useful tool in text processing, and grep can only implement a lookup function, and SED can not only find but also replace.
When SED is processed, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file.
Note: When SED is processed, the file content does not change unless you use redirection to store the output.
Sed-n ' 5 ' p test.txt---> Print line 5th
Sed-n ' 1,5 ' p test.txt---> Print 第1-5 line
Sed-n ' 1,$ ' P test.txt---> Print all Lines
Sed-n '/root/' P test.txt---> Print a row containing root
Sed-n '/^1/' P test.txt---> Print all lines beginning with 1
Sed-n '/in$/' P test.txt---> Print all lines in the end
Sed-e ' 1 ' p-e '/222/' p-n test.txt---> Print line 1th and line containing 222
Sed-n '/o\+t/' P test.txt---> Print contains O.. Line of T
Sed-nr '/o+t/' P test.txt---> Print contains O.. The line of T, where the r option is similar to the-e option of grep, plus the ability to avoid the ideographic character
Sed-nr '/root|sshd/' P test.txt---> Print rows containing root or sshd
9.5 sed (bottom)
Sed-n '/bus/' Ip test.txt---> Print rows with bus (case-matched), where i option means case insensitive
Sed ' 1 ' d test.txt---> Delete line 1th (the file itself does not change)
Sed '/root/' d test.txt---> delete the row containing the root (the file itself does not change)
Sed ' 1,3 ' d test.txt---> Delete 第1-3 rows (the file itself does not change)
Sed-i ' 1,3 ' d test.txt---> delete the 第1-3 line of the file ( plus the i option, the file changes )
Sed ' s/[0-9]//g ' test.txt---> Delete all numbers in a document
Sed ' s/[a-za-z]//g ' test.txt---> Delete all letters in a document
Sed ' 1,10s/ot/to/g ' test.txt---> Replace ot in 第1-10 line to
Sed-r ' s/([^:]+):(. *):([^:]+]/\3:\2:\1/' test.txt---> will be replaced with: All rows of the split line at the beginning and end of each line
Sed-r ' s/(. *)/aaa:&/' test.txt---> Add string aaa before all lines:
2018-1-16 Linux Learning notes [important]