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

要附加文本,使用符号a \,可以将指定文本一行或多行附加到指定行。如果不指定文本放
置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附
加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行
文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接
输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s e d命令编辑它。这时文件

的内容又被移至缓冲区。附加操作格式如下:

[address] a\

text\

text\

......

text

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

创建sed脚本文件:

要创建脚本文件a p p e n d . s e d.

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

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

[root@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.

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


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

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

[root@localhost huangcd]# cat append.sed 
#!/bin/sed -f
4 i\
Utter confusion followed.
[root@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。内容如下:

[root@localhost huangcd]# cat change.sed 
#!/bin/sed -f
#change.sed
/honeysuckle/  c\
The Office Dibble band played well.
[root@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.

像插入动作一样,可以使用行号代替模式,两种方式完成相同的功能。

[root@localhost huangcd]# cat change.sed 
#!/bin/sed -f
#change.sed
#/honeysuckle/  c\
3 c\
The Office Dibble band played well.
[root@localhost huangcd]# change.sed quot
-bash: change.sed: command not found
[root@localhost huangcd]# ./change.sed quote.txt 
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The Office Dibble band played well.
The local nurse Miss P.Neave was in attendance.

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

[root@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?
[root@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.
[root@localhost huangcd]# ./ mix.sed  quote.txt 
bash: ./: is a directory
[root@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意为删除第一行。

[root@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.

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

删除最后一行
[root@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’的行。

[root@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替换它。

[root@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 中删除$ 符号(记住这是一个特殊符号,必须用\ 屏蔽其特殊含义)。

[root@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
替换成Wow!hahahahahaha。

[root@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:

[root@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.

注意要将文件名括在s e d的单引号里。文件结果如下:

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

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

[root@localhost huangcd]# sed -n 's/nurse/"Hello" &/p'  quote.txt 
The local "Hello" nurse Miss P.Neave was in attendance.    //在nurse前面插入“Hello”

原句是The honeysuckle band played all night long for only $90。相信这种修改动作已经讲
解得很清楚了。

将sed结果写入文件命令:

像使用>文件重定向发送输出到一个文件一样,在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。

[root@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.
[root@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。

[root@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.
[root@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 /后放置附加文本。本
例为第三行。注意所读的文件名需要用单引号括起来。

[root@localhost huangcd]# cat sedex.txt 
Boom boom went the music.
[root@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次或任意多次。
查看文本文件,然后在下列行产生下列单词:

[root@localhost huangcd]# sed '/.a.*/q' quote.txt 
The honeysuckle band played all night long for only $90.
[root@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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值