sed命令

基于行的操作,对STDIN的数据进行替换、删除、新增等操作

参数如下

-n  安静模式。默认情况下,sed会输出所有内容,包括了修改和未修改的内容。使用-n则只输出修改过的那一行内容。


-r  支持扩展正则表达式


-i  直接修改文件,不输出内容

参数-i  直接对文件操作,不会输出任何内容。

 sed -i '$aEND LINE' regular_express.txt

$是最后一行的意思,用于指定范围。a则是插入动作。END LINE是一行内容

[root@vmtest ~]# cat regular_express.txt

"Open Source" is a good mechanism to develop programs.

apple is my favorite food.

……

gNu

END LINE



行范围

n1,n2  可选,指定sed的动作只针对从n1行到n2


动作

a  新增,在范围内的每一行下添加一行内容,例子

[root@vmtest ~]# cat -n regular_express.txt |sed '1,3aNew line

     1  "Open Source" is a good mechanism to develop programs.

New line

     2  apple is my favorite food.

New line

     3  Football game is not use feet only.

New line

     4  this dress doesn't fit me.

     5  However, this dress is about $ 3183 dollars.

     6  GNU is free air not free beer.

     7  Her hair is very beauty.

     8  I can't finish the test.

范围设置在第1到3行,每一行下方插入一行内容New Line



c  替换,如果有指定范围,则把整个范围替换成一行指定的内容;如果不指定范围,则把STDIN的每一行替换成指定内容。

[root@vmtest ~]# cat -n regular_express.txt |sed '1,3cReplace line'

Replace line

     4  this dress doesn't fit me.

     5  However, this dress is about $ 3183 dollars.

     6  GNU is free air not free beer.

     7  Her hair is very beauty

……

可以看到第1-3行被替换成一行


[root@vmtest ~]# cat -n regular_express.txt |sed 'cReplace line'   

Replace line

Replace line

Replace line

Replace line

……

每一行都被替换


d  删除,根据条件,删除整行

先看未处理的文件内容

[root@vmtest ~]# cat -n regular_express.txt            

     1  "Open Source" is a good mechanism to develop programs.

     2  apple is my favorite food.

     3  Football game is not use feet only.

     4  this dress doesn't fit me.

     5  However, this dress is about $ 3183 dollars.

     6  GNU is free air not free beer.


[root@vmtest ~]# cat -n regular_express.txt |sed '/3/d'

     1  "Open Source" is a good mechanism to develop programs.

     2  apple is my favorite food.

     4  this dress doesn't fit me.

     6  GNU is free air not free beer.

/与/之间是正则条件,只有那行的内容里包含有3这个字符,则整行删除。在原来的内容里第三行包含了行号3,而第5行里包含了$ 3183,所以着两行被删掉了。

另外,如果只指定范围,则把范围的内容删除

[root@vmtest ~]# cat -n regular_express.txt |sed '1,4d'

     5  However, this dress is about $ 3183 dollars.

     6  GNU is free air not free beer.

但貌似范围与条件不能合着来删除,


i  插入,与a相似,不过a是在原有行下插入一行,而i则是在原有行前插入。


p  打印,配合-n安静模式参数,和范围,可轻松实现打印文件的指定行范围内容

[root@vmtest ~]# cat -n regular_express.txt |sed -n '2,5p'

     2  apple is my favorite food.

     3  Football game is not use feet only.

     4  this dress doesn't fit me.

     5  However, this dress is about $ 3183 dollars.


s///g  替换,如配合范围使用则在范围内替换。

[root@vmtest ~]# cat -n regular_express.txt |sed 's/good/------------/g'            

     1  "Open Source" is a ------------ mechanism to develop programs.

     2  apple is my favorite food.

     3  Football game is not use feet only.

     4  this dress doesn't fit me.

     5  However, this dress is about $ 3183 dollars.

     6  GNU is free air not free beer.

     7  Her hair is very beauty.

     8  I can't finish the test.

     9  Oh! The soup taste ------------.

    10  motorcycle is cheap than car.