分类:
2005-08-30 16:14:09
引用: |
• 抽取域。 • 匹配正则表达式。 • 比较域。 • 增加、附加、替换。 • 基本的s e d命令和一行脚本。 |
1 sed怎样读取数据
s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
2 调用sed
调用s e d有三种方式:在命令行键入命令;将s e d命令插入脚本文件,然后调用s e d;将s e d命令插入脚本文件,并使s e d脚本可执行。
使用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 延续到下一输入行;允许跨行的模式匹配语句 |
代码: |
[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. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '2p' quote.txt It was an evening of splendid music and company. |
代码: |
[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. |
代码: |
[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. |
代码: |
[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. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt The local nurse Miss P.Neave was in attendance. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '/$/p' quote.txt The honeysuckle band played all night long for only $90. |
代码: |
[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. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt It was an evening of splendid music and company. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '1p' quote.txt The honeysuckle band played all night long for only $90. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '$p' quote.txt The local nurse Miss P.Neave was in attendance. |
代码: |
[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. |
代码: |
[sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt 2 |
代码: |
[sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/=' quote.txt It was an evening of splendid music and company. 2 |
代码: |
[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/aThen 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/iUtter 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/cThe 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 $ 00332##LPSO##23.14 $ 01299##USPD##34.46 $ |
代码: |
[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' |
代码: |
[sam@chenwy sam]$ cat data.txt Database Size(MB) DataCreated ----------------------------- GOSOUTH 2244 12/11/97 TRISUD 5632 8/9/99 (2 rows affected) |
代码: |
[sam@chenwy sam]$ cat data.txt |sed 's/--*/ /g' | sed '/^$/d' | sed '$d' | sed '1d' | awk '{print $1}' GOSOUTH TRISUD |
代码: |
[sam@chenwy sam]$ cat ok.txt AC456 AC492169 AC9967 AC88345 |
代码: |
[sam@chenwy sam]$ sed 's/[0-9][0-9]*/& Passed/g' ok.txt AC456 Passed AC492169 Passed AC9967 Passed AC88345 Passed |
代码: |
[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 |
代码: |
[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 |
引用: |
‘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键 |
代码: |
[sam@chenwy sam]$ echo $PWD |sed 's/^///g' usr/sam |
代码: |
[sam@chenwy sam]$ echo "Mr Willis" |sed 's/Mr /& Bruce/g' Mr BruceWillis |
代码: |
[sam@chenwy sam]$ echo "accounts.doc" |sed 's/^.//g' ccounts.doc |
代码: |
[sam@chenwy sam]$ echo "accounts.doc"|sed 's/.doc//g' accounts |
代码: |
[sam@chenwy sam]$ echo "accounts"|sed 's/$/.doc/g' accounts.doc |
代码: |
[sam@chenwy sam]$ x="Department+playroll&Building G" [sam@chenwy sam]$ echo $x Department+playroll&Building G |
代码: |
[sam@chenwy sam]$ echo $x |sed 's/+/ of /g' |sed 's/&/ Located at /g' Department of playroll Located at Building G |
代码: |
$ sed -e 'd' /etc/services |
代码: |
$ sed -e '1d' /etc/services | more |
代码: |
$ sed -e '1,10d' /etc/services | more |
代码: |
$ sed -e '/^#/d' /etc/services | more |
引用: |
字符 描述 与行首匹配 与行末尾匹配 与任一个字符匹配 将与前一个字符的零或多个出现匹配 [ ] 与 [ ] 之内的所有字符匹配 |
引用: |
规则 表达式 描述 /./ 将与包含至少一个字符的任何行匹配 /../ 将与包含至少两个字符的任何行匹配 /^#/ 将与以 '#' 开始的任何行匹配 /^$/ 将与所有空行匹配 /}^/ 将与以 '}'(无空格)结束的任何行匹配 /} *^/ 将与以 '}' 后面跟有零或多个空格结束的任何行匹配 /[abc]/ 将与包含小写 'a'、'b' 或 'c' 的任何行匹配 /^[abc]/ 将与以 'a'、'b' 或 'c'开始的任何行匹配 |
代码: |
$ sed -e '/^#/d' /etc/services | more |
代码: |
$ sed -n -e '/^#/p' /path/to/my/test/file | more |
代码: |
$ sed -n -e '/BEGIN/,/^END/p' /my/test/file | more |
代码: |
$ sed -n -e '/main[[:space:]]*(/,/^}/p' sourcefile.c | more |
代码: |
$ sed -e 's/foo/bar/' myfile.txt |
代码: |
$ sed -e 's/foo/bar/g' myfile.txt |
代码: |
$ sed -e '1,10s/enchantment/entrapment/g' myfile2.txt |
代码: |
$ sed -e '/^$/,/^END/s/hills/mountains/g' myfile3.txt |
代码: |
$ sed -e 's:/usr/local:/usr:g' mylist.txt |
代码: |
$ sed -e 's/usr/local/usrg' mylist.txt |
代码: |
$ sed -e 's/<.*>//g' myfile.html |
代码: |
This is what I meant. 变成: meant. 我们要的不是这个,而是: This is what I meant. |
代码: | <