Learn a little shell every day: Linux Three Musketeers-sed command

sed usage

The usage is as follows:

sed [选项]... {
    
    脚本(如果没有其他脚本)} [输入文件]...
Options Explanation
-n --Quiet, --silent, silent mode, do not output content to the screen
-e --Expression script, add "script" to the running list of the program
-f --File script file, add "script file" to the running list of the program
-r --Regexp-exended, use extended regular expressions in scripts
-i -In-place, edit files directly
-i.bak Back up files and edit them in place

Commonly used editing commands:

Keyword Explanation
d Delete matching rows and immediately enable the next round of loop
p Print the contents of the current mode space and append it to the default output
a Newly added, a string can be followed by a, support the use of \n to achieve multi-line append
i Insert text before the line
c Replace single or multiple lines of text
w Save the lines matching the pattern to the specified file
r Read the text of the specified file to the matched line in the pattern space
= Print line number of lines in pattern space
! Inversion of matching rows in pattern space
s/// Find and replace, support the use of other separators, s@@@,s###. Replacement mark: 1.g global replacement in the line 2.p shows the successfully replaced line 3.w newFile saves the successfully replaced line to the file

sed instance usage

The content of the test file is as follows

[root@hadoop-master test-sed]# nl file.txt
     1	leo hello-sed
     2	leo2 hello2-sed
     3	leo3 hello3-sed
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333

1. Print the second line of the file

[root@hadoop-master test-sed]# nl file.txt | sed -n 2p
     2	leo2 hello2-sed

2. Print lines 2-5 of the file

[root@hadoop-master test-sed]# nl file.txt | sed -n '2,5p'
     2	leo2 hello2-sed
     3	leo3 hello3-sed
     4	hello se
     5	aaa 111

3. Delete the second line

[root@hadoop-master test-sed]# nl file.txt | sed '2d'
     1	leo hello-sed
     3	leo3 hello3-sed
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333

4. Delete lines 2-5

[root@hadoop-master test-sed]# nl file.txt | sed '2,5d'
     1	leo hello-sed
     6	bbb 222
     7	ccc 333

5. Delete the second to last line

[root@hadoop-master test-sed]# nl file.txt | sed '2,$d'
     1	leo hello-sed

6, added “love china”

[root@hadoop-master test-sed]# nl file.txt | sed '2a love china\n'
     1	leo hello-sed
     2	leo2 hello2-sed
love china

     3	leo3 hello3-sed
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333
[root@hadoop-master test-sed]# nl file.txt | sed '2i love china'
     1	leo hello-sed
love china
     2	leo2 hello2-sed
     3	leo3 hello3-sed
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333

7. Replace lines 2-5 with "china"

[root@hadoop-master test-sed]# nl file.txt | sed '2,5c  china'
     1	leo hello-sed
china
     6	bbb 222
     7	ccc 333

8. Only print lines containing templates

[root@hadoop-master test-sed]# nl file.txt | sed -n '/leo/p'
     1	leo hello-sed
     2	leo2 hello2-sed
     3	leo3 hello3-sed

9. Only print the lines of the package without templates

[root@hadoop-master test-sed]# nl file.txt | sed '/leo/d'
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333

10. Replace string

Replace leo with china

[root@hadoop-master test-sed]# nl file.txt | sed 's/leo/china/g'
     1	china hello-sed
     2	china2 hello2-sed
     3	china3 hello3-sed
     4	hello se
     5	aaa 111
     6	bbb 222
     7	ccc 333

Guess you like

Origin blog.csdn.net/u011047968/article/details/108742279