#上古神器sed命令(下) [toc] ###测试文档:(people.txt) ``` Jackis 18-year old, he comes from US. Mikeis 16-y

来源:互联网 发布:淘宝客服招聘网 编辑:程序博客网 时间:2024/05/01 14:21
#上古神器sed命令(下)[toc]###测试文档:(people.txt)```Jack is 18-year old, he comes from US.Mike is 16-year old, he comes from Canada.Chen is 21-year old, he comes from China.Lau is 18-year old, he comes from HongKong.Michael is 20-year old, he comes from UK.Phoebe is 18-year old, she comes from Australie.```####10,提前预读多一行缓冲来进行匹配:```sed 'N;s/is/IS/' people.txt``````Jack IS 18-year old, he comes from US.Mike is 16-year old, he comes from Canada.Chen IS 21-year old, he comes from China.Lau is 18-year old, he comes from HongKong.Michael IS 20-year old, he comes from UK.Phoebe is 18-year old, she comes from Australie.```>注意:由于替换只会针对第一个出现的单词is,而通过N又多读了一行,因此这个命令的结果是只会替换奇数行。####11,在指定行的前面插入(i)或者后面插入(a)一些信息:`sed '3i abcd' people.txt` 意思是==>在第3行的前面插入abcd`sed '2a abcd' people.txt` 意思是==>在第2行的后面插入abcd`sed '1,4a abcd' people.txt` 意思是==>在第1至4行的后面分别插入abcd`sed '/US/a abcd' people.txt` 意思是==>在匹配US的行的后面插入abcd####12,将指定的行替换成其他信息:`sed "2c ok" people.txt` 意思是==>将第2行替换成ok####13,将指定的行删除掉:`sed ‘2d’ people.txt` 意思是==>将第2行给删掉`sed ‘/US/d’ people.txt` 意思是==>将匹配/US/的所有行给删掉`sed '/\<he\>/d' people.txt` 意思是==>将匹配he的所有行给删掉,>注意:之所以要用<>将he给括起来,是因为不想匹配she,当然,<>需要转义,写成\<\>####14,打印指定匹配的行,用命令p:`sed '/Chen/p' people.txt -n` 意思是==>打印匹配Chen的行`sed '/Chen/, /Lau/p' people.txt -n` 意思是==>打印匹配Chen或者Lau的行`sed '3,/UK/p' people.txt -n` 意思是==>从第3行开始打印,直到匹配UK为止`sed '/UK/,6p' people.txt -n` 意思是==>从匹配UK的行开始打印,直到第6行为止####15,使用相对位置:`sed '/US/, +2p' people.txt -n` 意思是==>打印匹配US的行,并打印其后的2行。####16,执行多个命令:`sed '{/he/{/18/p}}' people.txt -n` 意思是==>匹配所有/he/的行之后,再匹配/18/的行,然后打印出来。
0 0
原创粉丝点击