sed初步

sed相关知识

sed是一个非交互式的文本编辑器,刻意地文本文件和标准输入进行编辑。sed可以一次性处理所有的编辑任务。 sed仅仅编辑源文件的副本,如果要保存编辑的结果则可以输出重定向至另一个文件。
三种使用sed的方法:

sed [option] 'sed command' inputfile
sed [option] -f sed_script inputfile
./sed_script inputfile

sed option

optionmeaning
-n不打印所有行到标准输出
-e将下一个字符串解析成sed编辑命令
-f正在调用sed脚本

sed使用定位文本行和sed编辑命令进行文本操作。
编辑编辑命令n的意义是读取下一个输入行。

sed定位

optionmeaning
xx行
x,yx到y行
/pattern/查询包含模式的行
/pattern1/pattern2/查询包含两个模式的行
/pattern/,x从与pattern匹配的行到x行之间的行
x,/pattern/从x行到与pattern匹配的行之间的行
x,y查询不包括x和y行号的行

更加详细的内容直接man sed.

sed追加文本

sed '追加位置a\text' file
a\表示换行追加文本。(后面插入)
正则表达式中的$表示行尾,但是在sed中却是最后一行的意思。
x,y!表示不在x,y行号范围内的行,值得注意的是!不能和/pattern/匹配。

sed插入文本

sed '插入位置i\text' file
i\表示在匹配行的前面插入。 (前面插入)

sed修改文本

sed '修改位置c\text' file

sed删除文本

sed '删除位置d' file

sed替换文本

sed 's/被替换字符串/新字符串/[option]'

optionmeaning
g替换所有的被替换字符串
p与-n结合仅打印替换行
w filename重定向输出

注意:进行替换时如果没有g选项,那么仅仅替换所有匹配行的第一个匹配项。
如果option是数字,那么代表替换第几次出现的匹配项

sed变换命令

sed 'y/origin text/new text/' file
变换命令即是逐个字符替换
注意:origin text和new text必须等长

命令组

多条命令在同一位置执行可以包含在{}中,用分号隔开,和多条-e命令相比减少代码量。

二级命令提示符

在Bourne shell中可以使用二级命令提示符进行多行命令的输入。Bash shell是Bourne shell的扩展。

sed -n '
> 2,3=
> 2,3p
> ' doc

sed小练习

练习文本doc:
《呼啸山庄》 14 Secret visits

  1 1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the
  2 whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had
  3 ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,
  4 what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I  
  5 hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think
  6  he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 
  7  worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’
  8 Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff.  ‘Miss Linton!’he called.There's
  9 something I must tell you!’

打印doc文本的2至4行:

$ sed -n '2,4p' doc
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I 

打印doc中含有关键字Cathy的行

$ sed -n '/Cathy/p' doc  
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,

打印doc中含有关键字Cathy的行及行号

$ sed -n -e '/Cathy/=' -e '/Cathy/p' doc 
# 等价于
$ sed '/Cathy/{=;p}' doc
2
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
3
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,

在第一行中追加内容”this is append context.”

$ sed -n -e '1a\this is append context' -e '1,2p' doc
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
this is append context
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 

在匹配the Grange garden的行下追加两行文本

hello world
I love linux.
sed脚本:

#!/bin/sed -f
/the Grange garden/a\
        hello world\
        I love linux.

执行:

./append.sed doc
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,
        hello world
        I love linux.
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I 
hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think
...

打印匹配the Grange garden的行到最后一行

$ sed -n -e '/the Grange garden/,$=' -e '/the Grange garden/,$p' doc
3
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,
4
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I 
5
hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think
6
 he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 
7
 worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’
8
Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff.  ‘Miss Linton!’he called.There's 
9
something I must tell you!’

在匹配the Grange garden的行上方插入文本

I love linux.

sed -n -e '/the Grange garden/i\I love linux' -e '/the Grange garden/p' doc
I love linux
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,

注意: 插入内容默认是打印的。

将匹配the Grange garden的行替换成I love linux

$ sed -n -e '1,4=' -e '/the Grange garden/c\I love linux' -e '1,4p' doc
1
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
2
whole winter,so Cathy only had me as a companion on her walksShe had become very quiet and sad since her relationship with Linton had 
3
I love linux
4
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I  

删除4至最后一行

$ cat delete.sed 
sed -e '4,$d' doc
$ chmod +x delete.sed 
$ ./delete.sed 
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘Cathy,

删除含有Linton的行,不区分大小写并保存结果

sed '/[Ll][Ii][Nn][Tt][Oo][Nn]/d' doc > read

替换所有Cathy成凯西,并w重定向到read文本

sed -n -e 's/Cathy/凯西/g' -e 'w read' doc
cat read

1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
whole winter,so 凯西 only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘凯西,
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I 
hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think
 he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 
 worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’
Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff.  ‘Miss Linton!’he called.There's 
something I must tell you!’

所有Cathy加上“()”

$ sed -n 's/Cathy/(&)/pg' doc
whole winter,so (Cathy) only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 
ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying.  ‘(Cathy),
$ sed -n 's/Cathy/(cathy)/pg' doc

将doc的5至7行写入到read文本中

sed -n '5,7 w read' doc

将doc1和doc2合并成一个新的文件,原文件不受影响

doc1:

this is doc1
end1

doc2:

this is doc2
end2

sed '$r doc2' doc1 > doc3

寻找doc中带有’E’开头的词的所有行

sed -n '/E.*/p' doc
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 
what's the matter,love?’I ashed.  “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’  ‘I 
 he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 

将doc中大写的“MECA”全都替换成相应的小写字符

sed 'y/MECA/meca/' doc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值