设为首页 收藏本站
查看: 1153|回复: 3

linux shell编程指南第十章------sed 用法介绍1

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2013-12-2 09:16:17 | 显示全部楼层 |阅读模式
要附加文本,使用符号a \,可以将指定文本一行或多行附加到指定行。如果不指定文本放
置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附
加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行
文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接
输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s e d命令编辑它。这时文件

的内容又被移至缓冲区。

地址指定一个模式或行号,定位新文本附加位置。a\ 通知s e d对a \后的文本进行实际附加
操作。观察格式,注意每一行后面有一斜划线,这个斜划线代表换行。s e d执行到这儿,将创
建一新行,然后插入下一文本行。最后一行不加斜划线, s e d假定这是附加命令结尾。
当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。
这里没有这样做,因为可以留给使用者自己编写,并且在一个脚本文件中写这样的语句更适
宜。现在马上讲述s e d脚本文件。另外,脚本可以加入空行和注释行以增加可读性。



现在查看其具体功能。第一行是s e d命令解释行。脚本在这一行查找s e d以运行命令,这里
定位在/ b i n。
第二行以/ c o m p a n y /开始,这是附加操作起始位置。a \通知s e d这是一个附加操作,首先
应插入一个新行。第三行是附加操作要加入到拷贝的实际文本。    a\表示在后面添加。

[iyunv@localhost huangcd]# cat append.sed
#!/bin/sed -f
/company/ a\
Then suddenly it happened.

[iyunv@localhost huangcd]# ./append.sed quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Then suddenly it happened.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.


插入命令类似于附加命令,只是在指定行前面插入。和附加命令一样,它也只接受一个地址。

下面例子在以a t t e n d a n c e结尾的行前插入文本utter confusion followed。  i\表示在前面插入。

[iyunv@localhost huangcd]# cat append.sed
#!/bin/sed -f
4 i\
Utter confusion followed.
[iyunv@localhost huangcd]# ./append.sed  quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
Utter confusion followed.
The local nurse Miss P.Neave was in attendance.


修改命令将在匹配模式空间的指定行用新文本加以替代, c\表示替换

将第一行The honeysuckle band played all night long for only $90替换为The office Di b b l e
band played well。首先要匹配第一行的任何部分,可使用模式‘ / H o n e y s u c k l e /’。s e d脚本文
件为c h a n g e . s e d。内容如下:

[iyunv@localhost huangcd]# cat change.sed
#!/bin/sed -f
#change.sed
/honeysuckle/  c\
The Office Dibble band played well.
[iyunv@localhost huangcd]# ./change.sed  quote.txt
The Office Dibble band played well.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.


可以对同一个脚本中的相同文件进行修改、附加、插入三种动作匹配和混合操作。
下面是一个带有注释的脚本例子。

[iyunv@localhost huangcd]# cat mix.sed
#!/bin/sed -f
#name:mix.sed


1 c\
The Double band were grooving.


/evening/ i\
They played some great tunes.


$ c\
Nurse Neave was too tipsy to help.


3 a\
Where was the nurse to help?
[iyunv@localhost huangcd]# cat quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[iyunv@localhost huangcd]# ./ mix.sed  quote.txt
bash: ./: is a directory
[iyunv@localhost huangcd]# ./mix.sed  quote.txt
The Double band were grooving.
They played some great tunes.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
Where was the nurse to help?
Nurse Neave was too tipsy to help.



s e d删除文本格式:
[ a d d r e s s [,a d d r e s s ] ] d
地址可以是行的范围或模式,让我们看几个例子。
删除第一行;1 d意为删除第一行。

[iyunv@localhost huangcd]# sed '1d' quote.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

删除一到三行。
[iyunv@localhost huangcd]# sed '1,3d' quote.txt
The local nurse Miss P.Neave was in attendance.

删除最后一行
[iyunv@localhost huangcd]# sed '$d' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.

也可以使用正则表达式进行删除操作。下面的例子删除包含文本‘ N e a v e’的行。

[iyunv@localhost huangcd]# sed '/Neave/d' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.


替换命令用替换模式替换指定模式,格式为:
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
s选项通知s e d这是一个替换操作,并查询p a t t e r n - t o - f i n d,成功后用r e p l a c e m e n t - p a t t e r n替
换它。
替换选项如下:
g 缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。
p 缺省s e d将所有被替换行写入标准输出,加p选项将使- n选项无效。- n选项不打印输出
结果。
w 文件名使用此选项将输出定向到一个文件。

让我们看几个例子。替换n i g h t为N I G H T,首先查询模式n i g h t,然后用文本N I G H T替换它。

[iyunv@localhost huangcd]# sed 's/night/NIGHT/' quote.txt
The honeysuckle band played all NIGHT long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

要从$ 9 0 中删除$ 符号(记住这是一个特殊符号,必须用\ 屏蔽其特殊含义)。

[iyunv@localhost huangcd]# sed 's/\$//' quote.txt
The honeysuckle band played all night long for only 90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

要进行全局替换,即替换所有出现模式,只需在命令后加g选项。下面的例子将所有T h e
替换成Wo w!。

[iyunv@localhost huangcd]# sed 's/The/Wow!hahahahahaha/g' quote.txt
Wow!hahahahahaha honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
Wow!hahahahahaha local nurse Miss P.Neave was in attendance.

将替换结果写入一个文件用w选项,下面的例子将s p l e n d i d替换为S P L E N D I D的替换结果
写入文件s e d . o u t:

[iyunv@localhost huangcd]# sed 's/splendid/SPLENDID/w sed.out'  quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of SPLENDID music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

[iyunv@localhost huangcd]# cat sed.out
It was an evening of SPLENDID music and company.

如果要附加或修改一个字符串,可以使用( &)命令,&命令保存发现模式以便重新调用
它,然后把它放在替换字符串里面。这里给出一个修改的设计思路。先给出一个被替换模式,
然后是一个准备附加在第一个模式后的另一个模式,并且后面带有&,这样修改模式将放在
匹配模式之前。例如, s e d语句s/nurse/"Hello"&/p 的结果如下:

[iyunv@localhost huangcd]# cat quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[iyunv@localhost huangcd]# sed -n 's/nurse/"Hello" &/p'  quote.txt
The local "Hello" nurse Miss P.Neave was in attendance.    //在nurse前面插入“Hello”


像使用>文件重定向发送输出到一个文件一样,在s e d命令中也可以将结果输入文件。格
式有点像使用替换命令:

[ a d d r e s s [,address]]w filename
‘w’选项通知s e d将结果写入文件。f i l e n a m e是自解释文件名。下面有两个例子。

文件q u o t e . t x t输出到屏幕。模式范围即1,2行输出到文件f i l e d t。

[iyunv@localhost huangcd]# sed '1,2 w filedt' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[iyunv@localhost huangcd]# cat file
file1      filedt     files.out  filetest   
[iyunv@localhost huangcd]# cat filedt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.

下面例子中查询模式N e a v e,匹配结果行写入文件f i l e d h t。

[iyunv@localhost huangcd]# sed '/Neave/ w dht'  quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[iyunv@localhost huangcd]# cat dht
The local nurse Miss P.Neave was in attendance.

处理文件时, s e d允许从另一个文件中读文本,并将其文本附加在当前文件。此命令放在
模式匹配行后,格式为:
address r filename
这里r通知s e d将从另一个文件源中读文本。f i l e n a m e是其文件名。
现在创建一个小文件s e d e x . t x t,内容如下:

将s e d e x . t x t内容附加到文件q u o t e . t x t的拷贝。在模式匹配行/ c o m p a n y /后放置附加文本。本
例为第三行。注意所读的文件名需要用单引号括起来。

[iyunv@localhost huangcd]# cat sedex.txt
Boom boom went the music.
[iyunv@localhost huangcd]# sed '/company./r sedex.txt' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Boom boom went the music.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.


有时需要在模式匹配首次出现后退出s e d,以便执行其他处理脚本。退出命令格式为:
address q
下面的例子假定查询模式/ . a . * /,意为任意字符后跟字符a,再跟任意字符0次或任意多次。
查看文本文件,然后在下列行产生下列单词:

[iyunv@localhost huangcd]# sed '/.a.*/q' quote.txt
The honeysuckle band played all night long for only $90.
[iyunv@localhost huangcd]# cat quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-10898-1-1.html 上篇帖子: linux shell编程指南第十章------sed 用法介绍 下篇帖子: linux shell编程指南第十一章------------合并与分割 linux

尚未签到

发表于 2013-12-12 20:21:06 | 显示全部楼层
没有结局的爱情、你总是坚持。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

尚未签到

发表于 2013-12-20 07:05:17 | 显示全部楼层
向日葵,赖着太阳而生存。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

尚未签到

发表于 2013-12-26 21:38:52 | 显示全部楼层
我不想理的人,便不理,我想理的人,却不理我。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表