Table of contents
1. Common usage of sed command
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
3. Replace the text that meets the conditions
3.2, first backup and then modify the data
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
2.1. Output qualified text (p means normal output)
sed -n '3p' ens33 #output the content of the third line
sed -n '3,5p' ens33 #output 3-5 lines of content
sed -n '3p;5p' ens33 #output the third and fifth lines
sed -n 'p;n' ens33 #output all odd lines sed -n '1~2p' ens33 can also be achieved
sed -n 'n;p' ens33 #output all even lines sed -n '2~2p' ens33 can also be achieved
sed -n '1,5{p;n}' ens33 #Output odd lines of 1-5 lines
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
sed -n '7,${n;p}' ens33 #Output even lines after 7 lines
sed -n '3,+5p' ens33 #Start from line 3, output continuously, that is, output the content of lines 3-8
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
sed -n '/the/p' test.txt #output the line containing the
sed -n '4,/the/p' test.txt #output the line containing the starting from the fourth line
sed -n '/the/=' test.txt #Output the line number of the line containing the, use (=) to output the line number
sed -n '/^PI/p' test.txt #Output the lines beginning with PI
sed -n '/[0-9]$/p' test.txt #output lines ending with numbers 0-9
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
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.
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
nl test.txt | sed '3,5d' #Delete 3-5 lines
nl test.txt | sed '/the/d' #Delete the line with the character, with the ! symbol means negation, that is, delete the line without the
Negation operation with "!"
sed '/^[az]/d' test.txt #Delete lines starting with az
sed '/\.$/d' test.txt #Delete lines ending with .
sed '/^$/d' test.txt #delete blank lines
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
sed '/the/i who am I' test.txt #Add who am I in the line above the line containing the
sed '/the/a who am I' test.txt #Add who am I in the next line of the line containing the
sed '3c ##########' test.txt #Replace the third line with ##########
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
sed 's/l/L/2' #Replace the second l of each line with L
sed 's/l/L/g' test.txt #Replace all l in the file with L
sed 's/o/ /g' test.txt #Replace all o with empty characters
sed 's/^/#/' test.txt #Add # in front of all lines
sed '/the/s/^/#/' test.txt #Add a # in front of the line containing the
sed '3, 5s/the/THE/g' test.txt #Replace the in lines 3-5 with THE
sed '/the/s/o/O/g' text.txt #Replace the o in the line containing the with O
3.2, first backup and then modify the data
sed -i.bak 's/SELINUX=disable/SELINUX=enfocing/' /etc/selinux/config
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
sed '1,5/{H;d};14G' test.txt #Append 1-5 lines to 14 lines
sed '/the/w out.file' text.txt #Save the line containing the as a file out.file
sed '/the/r /etc/hostname' text.txt #Add the contents of the file /etc/hostname to each line containing the
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
sed '/the/a #####' text.txt #Insert a new line after each line containing the, the content is New
sed '3a #####\n ######' text.txt //Insert multiple lines of content after the third line, the middle \n means newline
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.
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
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
is constant
we add -i
sed -f sed.sh -i test.txt
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