sed高级玩法

第一个,同样是替换某字符,但是某一行我不想替换

比如下面有not的那一行不替换black

这里我遇到一个问题,/有用,#失效

另外需要用到-e参数

[root@data-1-1 ~]# cat 2.txt 
the black cat was chased by the brown dog.
the black cat was not chased by the brown dog
[root@data-1-1 ~]# sed -e '/not/s/black/white/g' 2.txt 
the black cat was chased by the brown dog.
the white cat was not chased by the brown dog
[root@data-1-1 ~]# sed -e '#not#s#black#white#g' 2.txt 
[root@data-1-1 ~]# sed -e '/not/s/black/white/g' 2.txt 
the black cat was chased by the brown dog.
the white cat was not chased by the brown dog
[root@data-1-1 ~]# 

  

 

使用多个-e参数替换多个不同的字符

>cat file
I have three dogs and two cats
>sed -e 's/dog/cat/g' -e 's/cat/elephant/g' file
I have three elephants and two elephants

  

[root@data-1-1 ~]# cat 1.txt 
111aaabbbcccddd222
[root@data-1-1 ~]# sed -e  s#aaa#AAA#g -e s#ccc#CCC#g 1.txt 
111AAAbbbCCCddd222
[root@data-1-1 ~]# 

  

 

多点编辑:e命令
$ sed -e '1,5d' -e 's/test/check/'example-----(-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除1至5行,第二条命令用check替换test。

命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

 

 

 

想将当前目录下 syn syn2 两个文件内的字符串AAAAAAA替换为660

ls syn* | xargs sed -i "s/AAAAAAA/660/g"

  

 

 

参考

https://chuna2.787528.xyz/xiaoleiel/p/8349564.html

http://www.panix.com/~elflord/unix/sed.html

 

posted on 2019-03-01 21:39  nmap  阅读(51)  评论(0)    收藏  举报

导航