Detailed explanation of the sed command of Shell Three Musketeers

Table of contents

1. Common usage of sed command

         options

         operate

2. Example of usage

2.1. Output qualified text (p means normal output)

2.2, sed combined with regular expressions to output the specified line

2.3. Delete the text that meets the conditions

 2.4. Insert qualified rows

3. Replace the text that meets the conditions

3.1, string replacement

3.2, first backup and then modify the data

4. Migrate qualified text

5. Edit files using scripts

Note: In addition to viewing the fixed number of lines, all other operations can be edited by adding the "-i" option, otherwise you can only see the results after the operation, and cannot directly modify the text.

 Summarize


        Sed is a powerful and simple file parsing and conversion tool that can read text, edit the text content (delete, replace, add, move, etc.) according to specified conditions, and finally output all lines or only some lines processed . sed can also implement quite complex text processing operations without interaction

1. Common usage of sed command

        Usually, there are two formats for invoking the sed command, as shown below. Among them, "parameter" refers to the target file of the operation. It is used when there are multiple operation objects. Process the input object file.

sed [options] 'action' argument

sed [options] -f scriptfile argument

         options

常见的sed命令选项包含以下几种:
-e或-expression=:表示用指定命令或者脚本来处理输入的文本文件
-f或-file-:表示用指定的脚本文件来处理输入的文件文件
-h或--help:显示帮助
-n、-quite或silent:表示仅表示处理后的结果
-i:直接编辑文本文件

         operate

        "Operation" is used only for actions that operate on files, that is, sed commands. Typically this is in the form of "[n1[n2]]" action parameters. n1 and n2 are optional, and represent the number of rows selected for operation. If the operation needs to be performed between 5 and 20 rows, it is expressed as "5, 20 action behaviors". Common operations include the following.

a:增加,在当前行下面增加一行指定内容。
c:替换,讲选定行替换为指定内容。
d:删除,删除选定的行。
i:插入,在选定行上面插入一行指定内容。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容,如果又非打印字符,则以ASCLL码输出。通常与“-n”选项一起使用。
s:替换,替换指定字符
y:字符转换

2. Example of usage

We copy a network card file and use it for demonstration

d34216764ba44d3a93d65264570f4054.png

2.1. Output qualified text (p means normal output)

sed -n '3p' ens33 #output the content of the third line

90e7080b1015480c91d988844774f0d5.png

sed -n '3,5p' ens33 #output 3-5 lines of content

 142766bb24bc49ababf2098a98fac847.png

sed -n '3p;5p' ens33 #output the third and fifth lines

51466d0c0041438c952c58f80fb7ba2a.png

sed -n 'p;n' ens33 #output all odd lines sed -n '1~2p' ens33 can also be achieved

26614c38a73349bab5e48a9bb4bb87bc.png

 sed -n 'n;p' ens33 #output all even lines sed -n '2~2p' ens33 can also be achieved

604b4884bf664c8885b052b1b38f96c9.png

 sed -n '1,5{p;n}' ens33 #Output odd lines of 1-5 lines

2e5c56e4c19f4a46bfdb6e70249732bd.png

sed -n '1,5{1~2p}' ens33 #Output 1-5 odd lines

sed -n '1,5{2~2p}' ens33 #Output the even lines of 1-5

8b8a5628e50b46f58b9cab68577f8c82.png

sed -n '7,${n;p}' ens33 #Output even lines after 7 lines

 4007c5cd63fa432294829eb4225da10e.png

sed -n '3,+5p' ens33 #Start from line 3, output continuously, that is, output the content of lines 3-8

42f6b3a04b6744848a9902df533eeb84.png

2.2, sed combined with regular expressions to output the specified line

The sed command has a slightly different format when combined with a regular expression, and the regular expression is surrounded by "/".
Note : If special symbols are encountered, the extended regular expression also needs to escape the character "\".

prepare a text file

fc4a56f5947348cfb2bd25464604d8bc.png

sed -n '/the/p' test.txt #output the line containing the 

88a0be4d3d124e58844770ed9f73a4f5.png

sed -n '4,/the/p' test.txt #output the line containing the starting from the fourth line

86dec058af704fce9d686106ab646e58.png

sed -n '/the/=' test.txt #Output the line number of the line containing the, use (=) to output the line number

2570305bf817472ba73f00a829f1bab3.png

sed -n '/^PI/p' test.txt #Output the lines beginning with PI

e6e718ba46b74dccb081a00de7c7d02b.png

sed -n '/[0-9]$/p' test.txt #output lines ending with numbers 0-9

 8df90bd6e19340c88f17b1db1a296675.png

Note: When looking at what ends with, pay attention to the trailing null character

sed -n '/\<wood\>/p' test.txt #Output the line containing the word wood, \< \> represents the word boundary

ec2b0ccb3f69454d8a763db44db0a6eb.png

2.3. Delete the text that meets the conditions

        Still use the test.txt file above, but we need to make a backup for recovery after deletion

        In the following commands, the nl command is used to count the number of lines in the file. Combining this command, you can view the results of the command execution more intuitively. We can also use -i or no option to display, but the difference is that -i directly operates on the text, and without parameters only displays the result and does not operate on the text.

 ba7f09f3df824626a15ae2e0c4472852.png

nl test.txt | sed '3d' or sed '3d' test.txt or sed -i '3d' test.txt

#-i will modify the text directly

#delete the third row

For better visualization, use nl

350912bc4b4e4cddaa2e5de6e1489117.png

nl test.txt | sed '3,5d' #Delete 3-5 lines

845eef4ed79b438d8ba745badd30b2a9.png

nl test.txt | sed '/the/d' #Delete the line with the character, with the ! symbol means negation, that is, delete the line without the

b8a958f278aa4feb87f61396753d9694.png

        Negation operation with "!" 

73d03cb8fb2148f9bb70cf63ccbfd771.png

sed '/^[az]/d' test.txt #Delete lines starting with az

c78ea1f277b94dd383820f874a015990.png

sed '/\.$/d' test.txt #Delete lines ending with .

026cb43d7dd144968eac49d5ed8f25ad.png

 sed '/^$/d' test.txt #delete blank lines

63ac5ee061c8439696ae68dcbf376829.png

        Note: If you want to delete repeated blank lines, that is, keep only one continuous blank line, execute the "sed -e '/^$/{n;/^$/d}' test.txt" command to achieve it. Its effect is the same as "cat -s test.txt", n means to read the next line of data. 

 2.4. Insert qualified rows

 sed '3i who am I' test.txt #Add who am I in the previous line of the third line

1a082527aaa24c1488f3bba95a2b9a8d.png

sed '/the/i who am I' test.txt #Add who am I in the line above the line containing the

4d798d64a21d44898db298f29d3562c5.png

sed '/the/a who am I' test.txt #Add who am I in the next line of the line containing the

924332def28743af80b3eca0cdaf9e99.png

 sed '3c ##########' test.txt #Replace the third line with ##########

2d3d0e9b18c54e8f9984f1f95d1b7d63.png

3. Replace the text that meets the conditions

3.1, string replacement

sed 's/the/THE' test.txt #Replace the first the in each line with THE

db50036cb22645e08a419e879172cf0d.png

sed 's/l/L/2' #Replace the second l of each line with L

b3509b3f8d1a42bbb6616eb64b1c6c23.png

sed 's/l/L/g' test.txt #Replace all l in the file with L

2f9cde32d75e48f4ba281576cc33c927.png

sed 's/o/ /g' test.txt #Replace all o with empty characters

a08c47010f5e47db92b83e425f760b2c.png

 sed 's/^/#/' test.txt #Add # in front of all lines

055821a06735427d88e8c4b92f9ce6c9.png

sed '/the/s/^/#/' test.txt #Add a # in front of the line containing the

abf6337cc70c490e948cc8044f175c4b.png

 sed '3, 5s/the/THE/g' test.txt #Replace the in lines 3-5 with THE

71b5a0289c4340a6b6d149c372f44682.png

 sed '/the/s/o/O/g' text.txt #Replace the o in the line containing the with O

875d669be0624124936f93fb40b7f781.png

3.2, first backup and then modify the data

sed -i.bak 's/SELINUX=disable/SELINUX=enfocing/' /etc/selinux/config

d358fe725fc7401888e89f16cc698402.png

4. Migrate qualified text

Parameters for migrating text

H:复制到剪贴板;
g、G:将剪贴板中的数据覆盖/追加至指定行;
w:保存为文件;
r:读取指定文件;
a:追加指定内容。具体操作方法如下所示。
I,i   忽略大小写

sed '/the/{H;d};$G' test.txt #The line containing the migrates to the end of the file, {;} is used for multiple operations

0a57e4c799f5448bb31ecd92b97ab56b.png

sed '1,5/{H;d};14G' test.txt #Append 1-5 lines to 14 lines

88fa6556b84f4601968e3faf6c1d7e19.png

 sed '/the/w out.file' text.txt #Save the line containing the as a file out.file

b8f8fbe095ed4ff88e2330c2c831a232.png

 sed '/the/r /etc/hostname' text.txt #Add the contents of the file /etc/hostname to each line containing the

6299ef7d5aaf460f8a865398e80e9c98.png

sed '3a #####' text.txt #Insert a new line after the third line, the content is ##### If it is the parameter i, it is on the third line

dc3b052ba2c940ee980198c11cb181fe.png

sed '/the/a #####' text.txt #Insert a new line after each line containing the, the content is New

24f83edf0d484f45bc8d2d05f568d3b6.png

 sed '3a #####\n ######' text.txt //Insert multiple lines of content after the third line, the middle \n means newline

cddf37d3c79c4f1d8496a0abdad7f9e1.png

5. Edit files using scripts

Use the sed script to store multiple editing instructions in a file (one editing instruction per line), and call it with the "-f" option.

6eb13abbb2e34ab2bae642ab2106f3a5.png
For example, execute the following command to transfer the contents of lines 1~5 to line 14, which is equivalent to sed '1,5{H;d};14G' text.txt

181b533da31a4503b96887fbfb767c64.png

Note: In addition to viewing the fixed number of lines, all other operations can be edited by adding the "-i" option, otherwise you can only see the results after the operation, and cannot directly modify the text.

For example, the modification of the text content above

Let's see the content after we finish the operation

93c78523cd5f4d6492748d4c20494548.png

is constant

we add -i

 sed -f  sed.sh -i test.txt   

830dc61bf7294dcda2d63e8cffe73666.png

 Summarize

        sed is a very useful text editing command, which allows users to edit the text without interaction when writing shell scripts, which greatly reduces the amount of human operations. need to study hard

Guess you like

Origin blog.csdn.net/qq_57377057/article/details/126216920