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

使用sed对文件进行操作

[日期:2012-12-07] 来源:Linux社区  作者:bobo12082119 [字体:大 中 小]
一.附加文本 
使用a\在指定行后面附加1行或多行;若不指定放置的位置,则默认放到每一行的后面。 
附加文本时,不允许指定范围,只允许一个地址模式。 
附加格式: 
[address] a\ 
text\ 
text\ 
... 
text 
注意: 
1.a\通知sed对a\后面的内容进行附加操作。 
2.每行后面都有"\",当sed执行到\时,将创建一个新行,并将内容添加进去。 
3.最后一行不能有"\"。 
例子: 
如果将附加的第一行最后的"\"去掉,那么运行脚本将会报错。 
pg append.sed 
#!bin/sed -f 
 
# append text 
/company/ a\ 
Then suddenly it happened. 
_yeeXun. 
执行之后: 
sed -f append.sed quote.txt 
sed: Unrecognized command: _yeeXun. 
 
修改之后 
pg append.sed 
#!bin/sed -f 
 
# append text 
/company/ a\ 
Then suddenly it happened.\ 
_yeeXun. 
执行脚本: 
sed -f append.sed quote.txt 
The honeysuckle band played all night long for noly $90. 
It was an evening of splendid music and company. 
Then suddenly it happened. 
_yeeXun. 
Too bad the disco floor fell through at 23:10. 
The local nurse Miss P.Neave was in attendance. 
 
因为sed不与原文件打交道,编辑的只是其一个拷贝,所以我们附加的数据不会写到文件中; 
当执行上面的命令之后,我们查看下quote.txt文件: 
pg quote.txt 
The honeysuckle band played all night long for noly $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. 
 
 
二.修改文本 
使用c\修改指定行,格式: 
[address[,address]] c\ 
text\ 
text\ 
... 
text 
address:行位置,可以使用正则表达式定位,也可以直接指定行号。 
text:为替换的文本,注意最后一行没有"\"。 
例子 
替换第三行数据: 
pg change.sed 
# change date 
/bad/ c\ 
[Two good.]\ 
next goods. 
运行脚本: 
sed -f change.sed quote.txt 
The honeysuckle band played all night long for noly $90. 
It was an evening of splendid music and company. 
[Two good.] 
next goods. 
The local nurse Miss P.Neave was in attendance.  linux
相关资讯       sed  sed使用  sed的使用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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