Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1351365
  • 博文数量: 245
  • 博客积分: 10021
  • 博客等级: 上将
  • 技术积分: 3094
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 14:51
文章存档

2011年(2)

2009年(152)

2008年(91)

我的朋友

分类:

2008-05-20 08:53:11



创建脚本文件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这是一个附加操作,首先应插入一个新行。
第三行是附加操作要加入到拷贝的实际文本。
输出显示附加结果。如果要保存输出,重定向到一个文件。

CODE:
[sam@chenwy sam]$ cat append.sed
#!/bin/sed -f
/company/ a\
Then suddenly it happed.

保存它,增加可执行权限,运行

CODE:
[sam@chenwy sam]chmod u+x append.sed
[sam@chenwy sam]$ ./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 happed.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

或直接用命令行:

CODE:
[sam@chenwy sam]$ sed "/company/a\Then suddenly it happened." 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:00.
The local nurse Miss P.Neave was in attendance.

[sam@chenwy sam]$ sed "/company/i\utter confusion followed." quote.txt
The honeysuckle band played all night long for only $90.
utter confusion followed.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

插入文本:
插入命令类似于附加命令,只是在指定行前面插入。和附加命令一样,它也只接受一个地址。
如在a t t e n d a n c e结尾的行前插入文本utter confusion followed。

CODE:
[sam@chenwy sam]$ sed "/company/i\Utter confusion followed." quote.txt

也可以指定行:

CODE:
[sam@chenwy sam]$ cat insert.sed
#!/bin/sed -f
4 i\
Utter confusion followed.

执行结果

CODE:
[sam@chenwy sam]$ chmod u+x insert.sed
[sam@chenwy sam]$ ./insert.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:00.
Utter confusion followed.
The local nurse Miss P.Neave was in attendance.

修改文本
修改命令将在匹配模式空间的指定行用新文本加以替代,格式如下:
将第一行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。内容如下:

CODE:
[sam@chenwy sam]$ cat change.sed
#!/bin/sed -f
3 c\
The office Dibble band played well.



CODE:
[sam@chenwy sam]$ chmod u+x change.sed
[sam@chenwy sam]$ ./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.

或命令行:

CODE:
[sam@chenwy sam]$ sed "/honeysuck/c\The Office Dibble band played well." 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:00.
The local nurse Miss P.Neave was in attendance.

可以对同一个脚本中的相同文件进行修改、附加、插入三种动作匹配和混合操作。

CODE:
[sam@chenwy sam]$ cat mix.sed
#!/bin/sed -f
1 c\
The Dibble band were grooving.

/evening/ i\
They played some great tunes.

3 a\
Where was the nurse to help?



CODE:
[sam@chenwy sam]$ chmod u+x mix.sed
[sam@chenwy sam]$ ./mix.sed quote.txt
The Dibble 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:00.
Where was the nurse to help?
The local nurse Miss P.Neave was in attendance.

删除文本
s e d删除文本格式:

CODE:
[ a d d r e s s [,a d d r e s s ] ] d

删除第一行;1 d意为删除第一行。

CODE:
[sam@chenwy sam]$ sed '1d' quote.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

删除第一到第三行:

CODE:
[sam@chenwy sam]$ sed '1,3d' quote.txt
The local nurse Miss P.Neave was in attendance.

删除最后一行:

CODE:
[sam@chenwy sam]$ 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:00.

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

CODE:
[sam@chenwy sam]$ 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:00.

替换文本
替换命令用替换模式替换指定模式,格式为:

CODE:
[ 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替换它。
替换选项如下:

QUOTE:
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替换它。

CODE:
[sam@chenwy sam]$ 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:00.
The local nurse Miss P.Neave was in attendance.

要从$ 9 0 中删除$ 符号(记住这是一个特殊符号,必须用\ 屏蔽其特殊含义),在r e p l a c e m e n t - p a t t e r n部分不写任何东西,保留空白,但仍需要用斜线括起来。在s e d中也可以这样删除一个字符串。

CODE:
[sam@chenwy sam]$ 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:00.
The local nurse Miss P.Neave was in attendance.

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

CODE:
[sam@chenwy sam]$ sed 's/The/Wow!/g' quote.txt
Wow! 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:00.
Wow! 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:

CODE:
[sam@chenwy sam]$ 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:00.
The local nurse Miss P.Neave was in attendance.

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

CODE:
[sam@chenwy sam]$ cat sed.out
It was an evening of SPLENDID music and company.
阅读(5428) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~