手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux Shell编程之sed编辑器&gawk程序

[日期:2011-05-07] 来源:51cto  作者:twentyfour [字体:大 中 小]

2.5、更该行
如上除了可以添加行之外还可以更该行,直接通过c命令即可实现。如下:
[root@www.linuxidc.com ~]# sed '3c\
> this is the changed number' test5
this is number 1
this is number 2
this is the changed number
this is number 4
如上通过指定行号,把第三行给替换了。实际上也可以通过指定文本模式替换:
[root@www.linuxidc.com ~]# sed '/ber 3/c\
> this is the changed number' test5
this is number 1
this is number 2
this is the changed number
this is number 4
由于ber 3出现在第三行的位置,所以该行被替换了。
在更改命令中,可以使用地址范围,不过匹配的多行会被一行或多行替换:
[root@www.linuxidc.com ~]# sed '2,3c\
> this is the changed number' test5
this is number 1
this is the changed number
this is number 4
你会发现如上只出现一行this is the changed number,而不是两行。这点注意下即可!

2.6、变换命令
变换命令(y)是唯一对单个字符进行操作的sed编辑器命令,其格式为:
y/inchars/outchars
变换命令中将inchars和outchars的值进行一一对应,如果inchars和outchars的值长度不同将会报错,如:
[root@www.linuxidc.com ~]# sed 'y/12/56/' test5
this is number 5
this is number 6
this is number 3
this is number 4
如上可以看到1被换成5、2被换成6,这样就实现了一一对应的变换
[root@www.linuxidc.com ~]# sed 'y/123/56/' test5
sed: -e expression #1, char 9: strings for `y' command are different lengths
这个是因为inchars和outchars的值长度不同导致的报错!

2.7、打印命令
先前使用p标记显示sed编辑器更改的行,还有如下命令用于打印来自数据流的信息:
* 打印文本行的p命令
* 打印文本行的=命令
* 打印文本行的l命令
2.7.1、打印行
p命令常用于打印 包含与文本模式匹配的文本行
[root@www.linuxidc.com ~]# sed -n '/number 1/p' test5
this is number 1
使用-n选项可以禁止所有其他行的输出,而仅打印包含匹配文本模式的行,这里打印包含有number 1的行。
[root@www.linuxidc.com ~]# sed -n '2,3p' test5
this is number 2
this is number 3
把第二行、第三行打印出来。当然也可以使用此提示符,并且在打印之前实现替换:
[root@www.linuxidc.com ~]# sed -n '/3/{
> p
> s/this/that/p
> }' test5
this is number 3
that is number 3
如上先实现把第三行的p效果,再者使用s替换命令把this改成that后p打印出来。
2.7.2、打印行号
使用=等号打印可以实现显示数据流当前行的行号。每次换行符出现在数据流中,sed编辑器就认为它结束了一行的文本。如下:
[root@www.linuxidc.com ~]# sed '=' test5
1
this is number 1
2
this is number 2
3
this is number 3
4
this is number 4
所以可以实现打印文本之前打印行号:
[root@www.linuxidc.com ~]# sed -n '/ber 2/{=;p}' test5
2
this is number 2
首先提取文本中匹配ber 2文本模式的行,然后通过等号打印匹配行的行号在打印出来。

2.8、将文件用于sed
sed编辑器命令允许处理文件而不必去替换文件,具体如下列出
2.8.1、写文件
w命令用于将文本行写入指定的文件
[root@www.linuxidc.com ~]# sed '1,2w test4' test5
this is number 1
this is number 2
this is number 3
this is number 4
如上表示把test5文件的第一二行保存到test4中去,如果test4文件不存在则创建之,原先就存在的话则把要保存的内容重定向到该文件中去覆盖原本的内容。当然,你可以使用-n选项使得STDOUT没显示这些行。
[root@www.linuxidc.com ~]# cat test4
this is number 1
this is number 2
可以看到匹配的文本行被保存到指定的文件中去,这里两个文件出于同一目录,否则可以指定绝对路径。
如果说需要基本文本值进行写文件也是同样的道理:
[root@www.linuxidc.com ~]# sed -n '/ber 3/w test4' test5
[root@www.linuxidc.com ~]# cat test4
this is number 3
把文本中含有ber 3的匹配行保存到test4中去,并且使得STDOUT不显示。
2.8.2、从文件读取数据
通过r命令可以实现sed编辑器在指定的匹配模式位置插入文件中的文本
[root@www.linuxidc.com ~]# cat insertfile
this is an added line
this is another added line
如上文件是准备被读取的文本文件
[root@www.linuxidc.com ~]# sed '2r insertfile' test5
this is number 1
this is number 2
this is an added line
this is another added line
this is number 3
this is number 4
指定从test5文件的第二行后面插入从文件insertfile中读取到的文本
当然,我们也可以使用文本匹配模式:
[root@www.linuxidc.com ~]# sed '/ber 3/r insertfile' test5
this is number 1
this is number 2
this is number 3
this is an added line
this is another added line
this is number 4
表示说从匹配ber 3文本的那一行后面插入从文件insertfile中读取到的文本
如果要添加在文本的末尾,想当然是使用美元符号了:
[root@www.linuxidc.com ~]# sed '$r insertfile' test5
this is number 1
this is number 2
this is number 3
this is number 4
this is an added line
this is another added line

如上所讲述的sed编辑器和gawk程序只是一个开始,下面还有通过讲述正则表达式,更加有针对性的来掌握sed编辑器和gawk程序的使用!!!

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款