- The SED software loops through a file or pipe to read a line, processing one line, and outputting one line.
- Mode space, the internal SED software is a temporary cache for storing the content read.
Software version
sed --versionGNU sed version 4.2.1
Syntax format
sed [option] [sed commands] [file]sed [选项] [sed命令] [输入文件]
SED options
| Options |
function |
| -N |
Using quiet mode, in general, all STDIN are output to the screen, adding-n will only print lines that have been specially processed by sed |
| -E |
Multiple edits, and command order affects results |
| -F |
Specify an sed script file to execute on the command line |
| -R |
Sed uses extended regular |
| -I. |
Directly modify the contents of the document read, not output on the screen |
SED command
| Command |
Description |
| A |
Add one or more rows after the current line |
| C |
Modify (replace) text in the current line with new text |
| D |
Delete Row |
| I |
Insert text before the current line |
| H |
Copy the contents of the pattern space to the staging buffer |
| H |
Append the contents of the pattern space to the staging buffer |
| G |
Take out the contents of the staging buffer, copy it to the pattern space, and overwrite the original content |
| G |
Take out the contents of the staging buffer, copy it to the schema space, append to the original content |
| L |
List non-printable characters |
| P |
Print Line |
| N |
Read in the next input line and start processing from the next command instead of the first command |
| Q |
End or exit sed |
| R |
Reading input rows from a file |
| ! |
Apply commands to all rows that are unexpected for the selected row |
| S |
Replace one string with another |
| G |
Global substitution within a row |
| P |
Print Line |
| W |
Writing rows to a file |
| X |
Swap the contents of the staging buffer with the pattern space |
| Y |
Converts a character to another character (cannot use the Y command on a regular expression) |
SED address range display
- SED software can process single or multiple lines.
- If you do not specify an address range before the SED command, all rows are matched by default.
- Usage: n1[,n2]{sed-commands}
- Addresses are separated by commas, and n1,n2 can be represented by numbers, regular expressions, or a combination of the two.
| Example |
function |
| 10i |
Action on line 10th |
| 10,20i |
For 10 to 20 rows, including 10th, 20 rows |
| 10,+20i |
For 10 to 30 (10+20) line operations, including 10th, 30 rows |
| 1~2i |
,...... to 1,3,5,7 Row operations |
| Ten, $i |
Action on 10 to last line ($ for last row), including line 10th |
| /oldboy/i |
Row operations on matching Oldboy |
| /oldboy/,/alex/i |
Rows to match Oldboy to the row operation of Alex |
| /oldboy/, $i |
Line-to-last line action on matching Oldboy |
| /oldboy/,10i |
The row to line 10th of the match Oldboy is displayed if the first 10 rows do not match to Oldboy, and 10 lines match Oldboy later. |
| 1,/alex/i |
Line action on line 1th to match Alex |
| /oldboy/,+2i |
2 rows to the following line to match Oldboy |
Add or delete change
- A--append text to the specified line
- I--insert text before the specified line
在第二行后面添加内容sed ‘2a 106,dandan,CSO‘ person.txt
在第二行插入内容sed ‘2i 106,dandan,CSO‘ person.txt
在第二行后面添加两行内容sed ‘2a 106,dandan,CSO\n107,bingbing,CCO‘ person.txt
By deleting
- D--delete the specified row
删除所有行sed ‘d‘ person.txt
删除第二行sed ‘2d‘ person.txt
删除第二到五行sed ‘2,5d‘ person.txt
删除从第三行开始到最后sed ‘3,$d‘ person.txt
删除有关键字zhangyao的行sed ‘/zhangyao/d‘ person.txt
删除有oldboy的行到包含Alex的行(范围)sed ‘/oldboy/,/Alex/d‘ person.txt
Change
- C-replace old rows with new lines
将第二行替换成其他内容sed ‘2c 106,dandan,CSO‘ person.txt
- S--replace one string with another
- G--global substitution within a row
- -I--Modify file contents
sed -i ‘s/内容1/内容2/g‘ oldboy.logsed -i ‘s#内容1#内容2#g‘ oldboy.log定界符/或#,也可以是任意符号如:或|等,但当替换内容包含定界符时,需转义。默认sed软件是对模式空间(内存中的数据)操作,而-i选项会更改磁盘上的文件内容。定界符第一个和第二个之间的就是被替换的内容(可以使正则表达式),第二个和第三个之间的就是替换后的内容(必须是具体内容)。
指定行修改内容sed ‘3s#0#9#‘ person.txt
指定变量的内容去替换sed "s#$x#$y#g" test.txteval sed ‘s#$x#$y#g‘ test.txt
用sed命令重命名当前目录下包含_finished的jpg文件ls *.jpg |sed -r ‘s#(^.*)_finished.*#mv & \1.jpg#g‘ |bashls *.jpg --列出所有的jpg文件sed -r --使用正则表达式匹配(^.*) --匹配任意字符串开头_finished.* --匹配包含_finished.,并任意字符串结尾mv & -- &是显示前面匹配到的结果\1 -- 显示前面括号内匹配到的内容|bash -- 以命令的形式执行替换后内容修改前stu_102999_1_finished.jpg 以命令的形式执行替换后内容mv stu_102999_1_finished.jpg stu_102999_1.jpg
Check
- P-Output specified content
- But it will output 2 matches by default.
- Use N to cancel default output
显示第二行内容sed -n ‘2p‘ person.txt
显示第二到第三行sed -n ‘2,3p‘ person.txt
显示所有行sed -n ‘p‘ person.txt
显示包含CTO的行sed -n ‘/CTO/p‘ person.txt
显示包含CTO的行到包含CFO的行(范围)sed -n ‘/CTO/,/CFO/p‘ person.txt
显示从第二行到包含CFO的行(范围)sed -n ‘2,/CFO/p‘ person.txt
04-linux Text Processing-sed