sed 用法介绍
From:http://blog.csdn.net/jinyonghe/article/details/1751513
引用: |
• 抽取域。 • 匹配正则表达式。 • 比较域。 • 增加、附加、替换。 • 基本的s e d命令和一行脚本。 |
代码: |
sed [选项] s e d命令输入文件。 |
代码: |
sed [选项] -f sed脚本文件输入文件 |
代码: |
s e d脚本文件[选项] 输入文件 |
引用: |
s e d选项如下: n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。 c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,此选项无用,但指定它也没有关系。 f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。 |
代码: |
$sed 'some-sed-commands' input-file > myoutfile |
引用: |
1) 使用行号,可以是一个简单数字,或是一个行号范围。 2 ) 使用正则表达式 |
代码: |
x x为一行号,如1 x , y 表示行号范围从x到y,如2,5表示从第2行到第5行 / p a t t e r n / 查询包含模式的行。例如/ d i s k /或/[a-z]/ / p a t t e r n / p a t t e r n / 查询包含两个模式的行。例如/ d i s k / d i s k s / p a t t e r n / , x 在给定行号上查询包含模式的行。如/ r i b b o n / , 3 x , / p a t t e r n / 通过行号和模式查询匹配行。3 . / v d u / x , y ! 查询不包含指定行号x和y的行。1 , 2 ! |
代码: |
sed编辑命令 p 打印匹配行 = 显示文件行号 a / 在定位行号后附加新文本信息 i / 在定位行号后插入新文本信息 d 删除定位行 c / 用新文本替换定位文本 s 使用替换模式替换相应模式 r 从另一个文件中读文本 w 写文本到一个文件 q 第一个模式匹配完成后推出或立即推出 l 显示与八进制A S C I I代码等价的控制字符 { } 在定位行执行的命令组 n 从另一个文件中读文本下一行,并附加在下一行 g 将模式2粘贴到/pattern n/ y 传送字符 n 延续到下一输入行;允许跨行的模式匹配语句 |
sed和正则表达式
s e d识别任何基本正则表达式和模式及其行匹配规则。记住规则之一是:如果要定位一特殊字符,必须使用( /)屏蔽其特殊含义
s e d例子中使用下述文本文件q u o t e . t x t。
代码: |
[sam@Linux_chenwy sam]$ 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:00. The local nurse Miss P.Neave was in attendance. |
1 使用p(rint)显示行
只打印第二行,用-n
代码: |
[sam@Linux_chenwy sam]$ sed -n '2p' quote.txt It was an evening of splendid music and company. |
2 打印范围
可以指定行的范围,现打印1到3行,用逗号分隔行号。
代码: |
[sam@Linux_chenwy sam]$ sed -n '1,3p' 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. |
3 打印模式
假定要匹配单词N e a v e,并打印此行,方法如下。使用模式/ p a t t e r n /格式,这里为/ N e a v e /。
代码: |
[sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt The honeysuckle band played all night long for only $90. The local nurse Miss P.Neave was in attendance. |
4 使用模式和行号进行查询
可以将行号和模式结合使用。假定要改动文件q u o t e . t x t最后一行中的单词t h e,使用s e d查询t h e,返回两行:
代码: |
[sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt The honeysuckle band played all night long for only $90. The local nurse Miss P.Neave was in attendance. |
使用模式与行号的混合方式可以剔除第一行,格式为l i n e _ n u m b e r, / p a t t e r n /。逗号用来分隔行号与模式开始部分。为达到预期结果,使用4 , / t h e /。意即只在第四行查询模式t h e,命令如下:
代码: |
[sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt The local nurse Miss P.Neave was in attendance. |
上面有错,其实是把第四行后的都打出来了
这个模式应该哪果指定行找不到符合条件的,就从下一行开始查找,直到找到为止,并把,找到行之前的全部打打印出来。
如果指定行本身就符合条伯,把本行及后面的行的全部打印出来
5 匹配元字符
匹配元字符$前,必须使用反斜线/屏蔽其特殊含义。模式为//$/ p。
代码: |
[sam@Linux_chenwy sam]$ sed -n '//$/p' quote.txt The honeysuckle band played all night long for only $90. |
6 显示整个文件
要打印整个文件,只需将行范围设为第一行到最后一行1 , $。$意为最后一行。
代码: |
[sam@Linux_chenwy sam]$ sed -n '1,$p' 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. |
7 任意字符
匹配任意字母,后跟任意字母的0次或多次重复,并以i n g结尾,模式为/ . * i n g /。可以使用这个模式查询以i n g结尾的任意单词。
代码: |
[sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt It was an evening of splendid music and company. |
8 首行
要打印文件第一行,使用行号:
代码: |
[sam@Linux_chenwy sam]$ sed -n '1p' quote.txt The honeysuckle band played all night long for only $90. |
9 最后一行
要打印最后一行,使用$。$是代表最后一行的元字符。
代码: |
[sam@Linux_chenwy sam]$ sed -n '$p' quote.txt The local nurse Miss P.Neave was in attendance. |
10 打印行号
要打印行号,使用等号=。打印模式匹配的行号,使用格式/ p a t t e r n / =。
代码: |
[sam@Linux_chenwy sam]$ sed -e '/music/=' quote.txt The honeysuckle band played all night long for only $90. 2 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. |
整个文件都打印出来,并且匹配行打印了行号。如果只关心实际行号,使用- e选项。
代码: |
[sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt 2 |
如果只打印行号及匹配行,必须使用两个s e d命令,并使用e选项。第一个命令打印模式匹配行,第二个使用=选项打印行号,格式为sed -n -e /pattern/p -e /pattern/=
代码: |
[sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/=' quote.txt It was an evening of splendid music and company. 2 |
P.S:新版本的sed(GNU sed version 4.0.5)带有-i选项,允许直接编辑修改文件内容而不需要重定向到临时文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/home/lee#cat file
new
abc
11 附加文本
要 附加文本,使用符号a /,可以将指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s e d命令编辑它。这时文件的内容又被移至缓冲区。
附加操作格式如下:
代码: |
[address]a/ text/ text/ ...... text |
代码: |
[sam@chenwy sam]$ cat append.sed #!/bin/sed -f /company/ a/ Then suddenly it happed. |
代码: |
[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. |
代码: |
[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 |
代码: |
[sam@chenwy sam]$ cat insert.sed #!/bin/sed -f 4 i/ Utter confusion followed. |
代码: |
[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. |
代码: |
[sam@chenwy sam]$ cat change.sed #!/bin/sed -f 3 c/ The office Dibble band played well. |
代码: |
[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. |
代码: |
[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. |
代码: |
[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? |
代码: |
[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. |
代码: |
[ a d d r e s s [,a d d r e s s ] ] d |
代码: |
[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. |
代码: |
[sam@chenwy sam]$ sed '1,3d' quote.txt The local nurse Miss P.Neave was in attendance. |
代码: |
[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. |
代码: |
[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. |
代码: |
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n] |
引用: |
g 缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。 p 缺省s e d将所有被替换行写入标准输出,加p选项将使- n选项无效。- n选项不打印输出结果。 w 文件名使用此选项将输出定向到一个文件。 |
代码: |
[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. |
代码: |
[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. |
代码: |
[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. |
代码: |
[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. |
代码: |
[sam@chenwy sam]$ cat sed.out It was an evening of SPLENDID music and company. |
代码: |
[sam@chenwy sam]$ sed -n 's/nurse/"hello" &/p' quote.txt The local "hello" nurse Miss P.Neave was in attendance. |
代码: |
[sam@chenwy sam]$ sed -n 's/played/from Hockering &/p' quote.txt The honeysuckle band from Hockering played all night long for only $90. |
代码: |
[ a d d r e s s [,address]]w filename |
代码: |
[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. |
代码: |
[sam@chenwy sam]$ cat filedt The honeysuckle band played all night long for only $90. It was an evening of splendid music and company. |
代码: |
[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. |
代码: |
[sam@chenwy sam]$ cat dht The local nurse Miss P.Neave was in attendance. |
代码: |
address r filename |
代码: |
[sam@chenwy sam]$ echo "Boom boom went the music" >sedex.txt [sam@chenwy sam]$ cat sedex.txt Boom boom went the music |
代码: |
[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. |
代码: |
address q |
代码: |
[sam@chenwy sam]$ sed '/.a.*/q' quote.txt The honeysuckle band played all night long for only $90. |
代码: |
[sam@chenwy sam]$ cat -v dos.txt 12332##DISO##45.12^M 00332##LPSO##23.14^M 01299##USPD##34.46^M |
代码: |
[ a d d r e s s,[ a d d r e s s ] ] l |
代码: |
[sam@chenwy sam]$ sed -n '1,$l' dos.txt 12332##DISO##45.12/r$ 00332##LPSO##23.14/r$ 01299##USPD##34.46/r$ |
代码: |
[sam@chenwy sam]$ sed 's/##/ /g' dos.txt 12332 DISO 45.12 00332 LPSO 23.14 01299 USPD 34.46 |
代码: |
[sam@chenwy sam]$ sed 's/##/ /g;s/^0*/ /g' dos.txt 12332 DISO 45.12 332 LPSO 23.14 1299 USPD 34.46 |
代码: |
[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 |
代码: |
[sam@chenwy sam]$ cat dos.txt | sed 's/^0*/ /g' | sed 's/^M/ /g' | sed 's/##/ /g' |