sed

  sed(stream editor,流编辑器),它一次处理一行内容,把当前处理的行存储在临时缓冲区,称为模式空间(pattern space),接着用 sed 命令处理缓冲区中的行,处理完成后,把缓冲区的行发送到屏幕。接着处理下一行,直到文件末尾。默认情况下,原文件内容没有改变。

Usage: sed [OPTION]... {script} [input-file]...

  -n:suppress automatic printing of pattern space
  -e script:add the script to the commands to be executed
  -f script-file:add the contents of script-file to the commands to be executed
  -i:edit files in place

原文件tmp.txt内容:

[root@localhost ~]# cat tmp.txt
#This is a example.
Hello World.
Apr 22 2020

打印

[root@localhost ~]# sed -n '2,3p' tmp.txt  #打印第二到第三行内容
Hello World.
Apr 22 2020

添加

[root@localhost ~]# sed '2aThis is a new line.' tmp.txt  #第二行后添加一行
#This is a example. 
Hello World. 
This is a new line. 
Apr 22 2020

删除

[root@localhost ~]# sed '1d' tmp.txt  #删除第一行
Hello World.
Apr 22 2020

替换

[root@localhost ~]# sed 's/\./!/g' tmp.txt  #将文件中的.全部替换为! 
#This is a example!
Hello World!
Apr 22 2020
posted @ 2020-04-20 10:22  PIPO2  阅读(152)  评论(0编辑  收藏  举报