sed 行对象【插入】【追加】【替换】

来源:互联网 发布:java程序员发展规划 编辑:程序博客网 时间:2024/04/30 01:30

sed行对象【插入】【追加】【替换】

All these three commands are for line object, not string.

一、Insert

i  newlinewords

insert one new line before line positioned by key word


linenumi newlinewords

insert one new line before line i

 [braveyly@m-net ~]$ cat sedaci.txt

word

word

word

[braveyly@m-net ~]$sed –e ‘1iStudent’ sedaci.txt

student

word

word

word

[braveyly@m-net ~]$sed –e ‘1,2iStudent’ sedaci.txt

student

word

student

word

word

[braveyly@m-net ~]$sed –e ‘/w/iStudent’ sedaci.txt

student

word

student

word

student

word

[braveyly@m-net ~]$sed –e ‘/d/i Student’ sedaci.txt

student

word

student

word

student

word

 

二、Apend

The syntax is completely the same with insert.

The difference is that append command puts one new line after line position.


[braveyly@m-net ~]$sed –e ‘/w/a Student’ sedaci.txt

word

student

word

student

word

student

 

三、Change

          The syntax is completely the same with insert and append.

 The difference is that change command replace the old line with the new line.

[braveyly@m-net ~]$ cat sedaci.txt

word

word

word

[braveyly@m-net ~]$sed –e ‘1cStudent’ sedaci.txt

student

word

word

[braveyly@m-net ~]$sed –e ‘/w/cStudent’ sedaci.txt

student

student

student