sed is a text-processing tool, data may be replaced, added, select certain other work
Format
sed options for action file name
Sed in the field using a domain replacement files
, such as the middle of writing a file has yaml fields need to be modified, can be used outside sed replace command line
format: sed 's / original character / new character /' file name
[root@zhaocheng ~]# sed 's/systemctl/service/' filetest
root:$1$dDTFylQ3$.vTZKpm7mrra9WMsxvBfW.:18241:0:99999:7
bin:*:17834:0:99999:7:dad
lp:*:17834:0:99999:7ada
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
nginx:!!:18289::::::daaf
rabbitmq:!!:18297:::::dada
service start mysqld
dadad:nginx:sdada
Above will only output to the screen, and does not modify the file, if a direct replacement can add -i
[root@zhaocheng ~]# sed -i 's/systemctl/service/' filetest
[root@zhaocheng ~]# cat filetest
root:$1$dDTFylQ3$.vTZKpm7mrra9WMsxvBfW.:18241:0:99999:7
bin:*:17834:0:99999:7:dad
lp:*:17834:0:99999:7ada
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
nginx:!!:18289::::::daaf
rabbitmq:!!:18297:::::dada
service start mysqld
dadad:nginx:sdada
Parameter Meaning sed command
常用命令:
a 新增
c 取代
d 删除
i 插入
p 列印
s 取代
To delete a row
if dealing with the text needs to be deleted some lines, you can use d, on behalf of deleted
without sed -i will not be deleted, only without the display on the screen
[root@zhaocheng]# sed '1d' filetest //删除第一行
[root@zhaocheng]# sed '$d' filetest //删除最后一行
[root@zhaocheng]# sed '1,2d' filetest //删除第一行到第二行
[root@zhaocheng]# sed '3,$d' filetest //删除第三行到最后一行
Display a line
such as dealing with the need to extract a text a line of text
[root@zhaocheng]# sed -n '1p' filetest //显示第一行
[root@zhaocheng]# sed -n '$p' filetest //显示最后一行
[root@zhaocheng]# sed -n '1,2p' filetest //显示第一行到第二行
[root@zhaocheng]# sed -n '3,$p' filetest //显示第三行到最后一行
Query usage patterns
such as dealing with larger text wanted to find out this field, you can directly use the -n parameter display, the action specified keywords need to extract, p print
[root@zhaocheng]# sed -n '/bin/p' filetest //查询包括关键字bin所在所有行
[root@zhaocheng]# sed -n '/\$/p' filetest
service start mysqld$
//查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义,不然识别不出
Add one or more rows string
such as the processing to add the text field, and added to a specified row, can be used a, is increased, 1a is going to increase in the first row, can be used if the field is used more in vim set nu , display line numbers, or directly from the command line using the cat -n direct line number, the command line using sed 'rows a xxxxxx file name
, a new row below the first row
[root@zhaocheng ~]# sed '1a sed is very useful' filetest
bin:*:17834:0:99999:7:dad
sed is very useful
lp:*:17834:0:99999:7ada
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
nginx:!!:18289::::::daaf
rabbitmq:!!:18297:::::dada
service start mysqld$
dadad:nginx:sdada%
Increase in the second row and the second row 3 sed is very useful
[root@zhaocheng ~]# sed '2,3a sed is very useful' filetest
bin:*:17834:0:99999:7:dad
lp:*:17834:0:99999:7ada
sed is very useful
sync:*:17834:0:99999:7:::gg
sed is very useful
Be substituted OK, here I will replace the first line kuberntes, 1c is the first line, c is replaced
[root@zhaocheng ~]# sed '1c kubernetes' filetest
kubernetes
lp:*:17834:0:99999:7ada
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
nginx:!!:18289::::::daaf
rabbitmq:!!:18297:::::dada
service start mysqld$
dadad:nginx:sdada%
Add the last line of the text jenkins, $ is the last line, a is added
[root@zhaocheng ~]# sed -i '$a jenkins' filetest
[root@zhaocheng ~]# cat filetest
hellp
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
dadad:nginx:sdada%
jenkins
Delete the line matching, matching jenkins this line, delete
[root@zhaocheng ~]# sed -i '/jenkins/d' filetest
[root@zhaocheng ~]# cat filetest
hellp
sync:*:17834:0:99999:7:::gg
shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
dadad:nginx:sdada%
Matching the text with a # and removed using ^ here beginning with # / d delete
[root@zhaocheng ~]# cat filetest
#hellp
#sync:*:17834:0:99999:7:::gg
#shutdown:*:17834:0:99999:7::da
halt:*:17834:0:99999:7::fsda
dadad:nginx:sdada%
[root@zhaocheng ~]# sed -i '/^#/d' filetest
[root@zhaocheng ~]# cat filetest
halt:*:17834:0:99999:7::fsda
dadad:nginx:sdada%