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

[Copy to clipboard] [ - ]

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 tN I G H T,首先查询模式n i g h t,然后用文本N I G H T替换它。

[Copy to clipboard] [ - ]

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中也可以这样删除一个字符串。

[Copy to clipboard] [ - ]

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!。

[Copy to clipboard] [ - ]

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

[Copy to clipboard] [ - ]

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的单引号里。文件结果如下:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat sed.out
It was an evening of SPLENDID music and company.

 

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

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed -n 's/nurse/"hello" &/p' quote.txt
The local "hello" nurse Miss P.Neave was in attendance.


原句是 文本行The local nurse Miss P.Neave was in attendance
记住模 式中要使用空格,因为输出结果表明应加入空格。

还有一 个例子:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed -n 's/played/from Hockering &/p' quote.txt
The honeysuckle band from Hockering played all night long for only $90.


原句是The honeysuckle band played all night long for only $90

sed结果写入文件命令
像使用>文件重定向发送输出到一个文件一样,在s e d命 令中也可以将结果输入文件。格式有点像使用替换命令:

[Copy to clipboard] [ - ]

CODE:

[ a d d r e s s [address]]w filename


‘w’
选 项通知s e d将 结果写入文件。f i l e n a m e是自解释文件名。
下面有 两个例子。

[Copy to clipboard] [ - ]

CODE:

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


文件q u o t e . t x t输出到屏幕。模式范围即12行输出到文件f i l e d t

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ 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

[Copy to clipboard] [ - ]

CODE:

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



[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat dht
The local nurse Miss P.Neave was in attendance.


从 文件中读文本
处理文 件时, s e d允许从另一个文件中读文本,并将其文本附加在当前文件。此命令放在模式匹配行后,格式 为:

[Copy to clipboard] [ - ]

CODE:

address r filename


这里r通知s e d将从另一个文件源中读文本。f i l e n a m e是其 文件名。

现在创 建一个小文件s e d e x . t x t,内容如下:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo "Boom boom went the music" >sedex.txt
[sam@chenwy sam]$ cat sedex.txt
Boom boom went the music


s e d e x . t x t内容附加到文件q u o t e . t x t的拷贝。在模式匹配行/ c o m p a n y /后 放置附加文本。本例为第三行。注意所读的文件名需要用单引号括起来。

[Copy to clipboard] [ - ]

CODE:

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


匹 配后退出
有时需 要在模式匹配首次出现后退出s e d, 以便执行其他处理脚本。退出命令格式为:

[Copy to clipboard] [ - ]

CODE:

address q


下面的 例子假定查询模式/ . a . * /,意为任意字符后跟字符a,再跟任意字符0次或任意多次。
查询首 次出现模式,然后退出。需要将q放在s e d语句末尾。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed '/.a.*/q' quote.txt
The honeysuckle band played all night long for only $90.

 


显 示文件中的控制字符

1
$vi dos.txt
进入vi后,用ctrl+v 再用ctrl+M产生控制字符^M不知对不对

使用cat -v filename命令查看编辑好的文件

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat -v dos.txt
12332##DISO##45.12^M
00332##LPSO##23.14^M
01299##USPD##34.46^M


s e d
格式为:

[Copy to clipboard] [ - ]

CODE:

[ a d d r e s s[ a d d r e s s ] ] l


‘l’
意 为列表。一般情况下要列出整个文件,而不是模式匹配行,因此使用l要从第一到最后一行。模式范围1$即为此意。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed -n '1,$l' dos.txt
12332##DISO##45.12\r$
00332##LPSO##23.14\r$
01299##USPD##34.46\r$


处 理控制字符
使用s e d实现的一个重要功能是在另一个系统中下载的文件中剔除控制字符。
下面是 传送过来的文件( d o s . t x t)的 部分脚本。必须去除所有可疑字符,以便于帐号所有者使用文件。

删除所 有的#字符很容易,可以使用全局替换命令。这里用一个空格替换两个或
更多的#符号。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed 's/##/ /g' dos.txt
12332 DISO 45.12
00332 LPSO 23.14
01299 USPD 34.46


。删除 所有行首的0。使用^符号表示模式从行首开始, ^ 0 *表示行首任意个0。模式s / ^ 0 * / / g设置替换部分为空,即为删除模 式,正是要求所在。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed 's/##/ /g;s/^0*/ /g' dos.txt
12332 DISO 45.12
332 LPSO 23.14
1299 USPD 34.46


最后去 除行尾^ M符号,为此需做全局替换。设置替换部分为空。模式为:
‘s / ^ m / / g’
注意‘^ M’,这是一个控制字符。
在 命令行里也必须用^M控制字符耶!?

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed 's/##/ /g;s/^0*/ /g;s/^M/ /g' dos.txt
12332 DISO 45.12
332 LPSO 23.14
1299 USPD 34.46


[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat dos.txt | sed 's/^0*/ /g' | sed 's/^M/ /g' | sed 's/##/ /g'

 


处 理报文输出
当从数 据库中执行语句输出时,一旦有了输出结果,脚本即可做进一步处理。通常先做一些整理,下面是一个s q l查询结果。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat data.txt
Database Size(MB) DataCreated
-----------------------------
GOSOUTH  2244     12/11/97
TRISUD   5632     8/9/99
(2 rows affected)


为了使 用上述输出信息做进一步自动处理,需要知道所存数据库名称,为此需执行以下
操作:
1)
使 用s / - * / / g删除横线- - - - - -
2)
使 用/ ^ $ / d删除空行。
3)
使 用$ d删除 最后一行
4)
使 用1 d删除 第一行。
5)
使 用awk {print $1}打印第一列。
命令如 下,这里使用了c a t,并管道传送结果到s e d命令。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat data.txt |sed 's/--*/ /g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}'
GOSOUTH
TRISUD


附 加文本
当帐户 完成设置一个文件时,帐号管理者可能要在文件中每个帐号后面加一段文字,下面是此类文件的一部分:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ cat ok.txt
AC456
AC492169
AC9967
AC88345


任务是 在每一行末尾加一个字符串‘ p a s s e d’
使用$命令修改各域会使工作相对容易些。首先需要匹配至少两个或更多的数字重复出现,这样将所有的帐号加进匹配模式。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ sed 's/[0-9][0-9]*/& Passed/g' ok.txt
AC456 Passed
AC492169 Passed
AC9967 Passed
AC88345 Passed


shellsed传值
要从命 令行中向s e d传值,值得注意的是用双引号,否则功能不执行。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ NAME="It's a go situation"
[sam@chenwy sam]$ REPLACE="GO"
[sam@chenwy sam]$ echo $NAME | sed "s/go/$REPLACE/g"
It's a GO situation


sed输出中设置shell变量
s e d输出中设置s h e l l变量是一个简单的替换过程。运用上 面的例子,创建s h e l l变量N E W- N A M E,保存上述s e d例 子的输出结果。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ NAME="It's a go situation"
[sam@chenwy sam]$ REPLACE="GO"
[sam@chenwy sam]$ NEW_NAME=`echo $NAME | sed "s/go/$REPLACE/g"`
[sam@chenwy sam]$ echo $NEW_NAME
It's a GO situation


这里的`是键盘左上角那个`


下 面是一些一行命令集。([ ]表示空格, [ ]表示t a b键)

QUOTE:

‘s / \ . $ / / g’ 删 除以句点结尾行
‘-e /abcd/d’
删除包含a b c d的行
‘s / [ ] [ ] [ ] * / [ ] / g’
删除一个以上空格,用一个空格代替
‘s / ^ [ ] [ ] * / / g’
删除行首空格
‘s / \ . [ ] [ ] * / [ ] / g’
删除句点后跟两个或更多空格,代之以一个空格
‘/ ^ $ / d’
删除空行
‘s / ^ . / / g’
删除第一个字符
‘s /CO L \ ( . . . \ ) / / g’
删除紧跟C O L的后三个字母
‘s / ^ \ / / / g’
从路径中删除第一个\
‘s / [ ] / [ ] / / g’
删除所有空格并用t a b键替代
‘S / ^ [ ] / / g’
删除行首所有t a b
‘s / [ ] * / / g’
删除所有t a b


1.
删 除路径名第一个\符号
将当前 工作目录返回给s e d,删除第一个\

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo $PWD |sed 's/^\///g'
usr/sam


2.
追 加/插入文本
"Mr Wi l l i s "字串 返回给s e d并 在M r后而 追加" B r u c e "

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo "Mr Willis" |sed 's/Mr /& Bruce/g'
Mr  BruceWillis


3.
删 除首字符
s e d
删除字符串“a c c o u n t s . d o c”首字符。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo "accounts.doc" |sed 's/^.//g'
ccounts.doc


4.
删 除文件扩展名
s e d
删除“a c c o u n t s . d o c”文件扩展名。

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo "accounts.doc"|sed 's/.doc//g'
accounts


5.
增 加文件扩展名
s e d
附加字符串“. d o c”到字符串“a c c o u n t s”

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo "accounts"|sed 's/$/.doc/g'
accounts.doc


6.
替 换字符系列
如果变 量x含有下列字符串:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ x="Department+playroll&Building G"
[sam@chenwy sam]$ echo $x
Department+playroll&Building G


如果要 加入of,located,并去掉+,&实现下列转换:

[Copy to clipboard] [ - ]

CODE:

[sam@chenwy sam]$ echo $x |sed 's/\+/ of /g' |sed 's/\&/ Located at /g'
Department of playroll Located at Building G


+ of 替换,&located at 替换