sed使用

sed命令

一、替换标记

s/pattern/replacement/flags

默认情况下只会替换每行的首次出现的内容,如果要替换其他位置需要使用flags

1、不使用flag

[root@bogon tmp]# cat data.txt 
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@bogon tmp]# sed 's/test/aaa/' data.txt 
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
[root@bogon tmp]# cat data.txt 
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

2、使用flags/pattern/replacement/flags

4种可用的替换标记:

  • 数字,表明新文本将替换第几处模式匹配的地方;
  • g,表明新文本将会替换所有匹配的文本;
  • p,表明原先行的内容要打印出来;
  • w file,将替换的结果写到文件中。 

1)数字型,比如数字2,替换第2次出现的内容,不会修改原文件

[root@bogon tmp]# sed 's/test/hhhh/2' data.txt
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.

2)g,替换所有的行,不会修改原文件

[root@bogon tmp]# sed 's/test/aaa/g' data.txt
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.

3)w file,将替换的结果写到新文件中,不会替换原文件

[root@bogon tmp]# sed 's/test/aaa/w aaa.txt' data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.

查看当前目录,多了文件aaa.txt
[root@bogon tmp]# ls
aaa.txt keyring-nBhNRc orbit-gdm pulse-PDnM11f40MzK VMwareDnD
data.txt keyring-o1ipCa orbit-root pulse-tvQCaCozIrxH

4)pp替换标记会打印与替换命令中指定的模式匹配的行

[root@bogon tmp]# cat aaa.txt
this is a aaa of the test script.
this is a sss of the test script.
[root@bogon tmp]# sed 's/aaa/test/p' aaa.txt
this is a test of the test script.
this is a test of the test script.
this is a sss of the test script.

这通常会和sed-n选项一起使用。
$ cat data5.txt
This is a test line.
This is a different line.


$ sed -n 's/test/trial/p' data5.txt
This is a trial line.

-n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是
只输出被替换命令修改过的行。

二、使用地址 

默认情况下,sed命令作用于文本数据的所有行。如果只想将命令作用于特定行或者某些行,则需要使用行寻址。

sed编辑器的2种行寻址方式:

  • 以数字形式表示行区间
  • 用文本模式来过滤出行

[address] command

address {

command1

command2

command3

}

1、数字方式行寻址

[root@localhost tmp]# sed '2s/test/hhh/' aaa.txt
this is a aaa of the test script.
this is a sss of the hhh script.

 

修改2-4行每行第2次匹配上的内容

[root@localhost tmp]# sed '2,4s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

 

修改第2行至最后一行的匹配的内容

[root@localhost tmp]# sed '2,$s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.

 

2、命令组合方式

[root@localhost ~]# sed '2,4{        修改2-4行的内容
> s/test/hhh/2                                修改每行的第2次匹配的内容
> s/a test/1111/                              修改每行的第1次匹配的内容
> }' /tmp/data.txt
this is a test of the test script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@localhost ~]#

3、删除行

$ sed '/number 1/d' data6.txt
This is line number 2.
This is line number 3.
This is line number 4.

 

 

 

 

 

 

 

 

 

posted @ 2018-12-11 21:28  静心_心静  阅读(253)  评论(0编辑  收藏  举报