现在的位置: 首页 > 综合 > 正文

24、sed用法

2011年04月20日 ⁄ 综合 ⁄ 共 3183字 ⁄ 字号 评论关闭

1、The sed editor is called a stream editor,,主要是根据表达式表述的条件,搜索到指定的内容,可以对内容进行修改,替换和删除,但是只是在输出的时候修改,并不会修改输入的文件的内容。A stream editor edits a stream of data based on a set of rules you supply ahead of time

2、sed的操作流程如下:

    It reads one line of data at a time from the input and matches that data with the supplied editor commands, changes data in the stream as specified in the commands, then outputs the new data to STDOUT.

2、命令

Sed [options] 'command' files

Sed [options] –f sedscript files

1)options

(1)-n : Don't produce output for each command, but wait for the print command.

(2) -f file : Add the commands specified in the file file to the commands run while processing the input.

(3) -e script Add commands specified in script to the commands run while processing the input.

2)commands

(1)替换

s/pattern/replacement/flags

flags可以是:

一个数字num:只替换第num次出现的pattern

g:替换所有

p:输出替换后的内容(只输出替换后的行)

w file:把替换的结果(只写替换的行)写入到文件中

sed 's/buf/buf4/2' fstatt.cpp

sed -n 's/\(La\)/\1Oo/p' dataf3 //把找到的La 存起来,用\1 取回来再使用。这个指令作用的结果:若资料列含有La 字符串,则第一个出现的La 会置换成LaOo,然后,再显示这些含有La 的资料列

sed 's/^*anonymous_enable=.*/anonymous_enable=YES/' test //^表示由列首开始比对,紧接着0 个以上的字符

(2)寻址

    默认下,the sed editor apply to all lines of the text data.我们可以通过寻址,把行数限制在指定行或几行内。

[address]command

sed '2,3s/dog/cat/' data1 //替换2~3

/pattern/command //在匹配pattern的行执行command

ed '/rich/s/bash/csh/' /etc/passwd //把匹配rich的行中bash替换成csh

还可以把命令打包:

address {

command1

command2

command3

}

    The sed editor applies each of the commands you specify only to lines that match the address specified.

sed '2{

> s/fox/elephant/

> s/dog/cat/

> }' data1

    在第2行执行该操作。

(3)删除

sed 'd' data1 //默认下,d将删除all of the lines will be deleted from the stream

sed '3d' data6 //删除第3

sed '2,3d' data6 //删除2~3

sed '/pattern/d' data6 //删除匹配pattern的行

sed  '/type.h/d' fstatt.cpp

sed -e '/beatu/!d' file //删除不含有xx的所有行

sed -e '/word1/,10/d' file //删除从含有word1的行到第10

sed -e '10,/word1/d' file

sed '/[0-9]\{3\}/d' dataf3 //把含有3位数的列删除

(4) 插入和附加

The insert command (i) adds a new line before the specified line.

The append command (a) adds a new line after the specified line.

sed '[address]command\ new line'

sed '1,3a\begin' fstatt.cpp //在1~3行后append begin

(5)改变整行的内容

sed '3c\begin' fstatt.cpp  //把第3行替换成begin

(6)输出行

sed -n '/pattern/p' data6

sed -n '1,3p' test

sed -n '1,3!p' test //输出非1~3行的数据

(7)写到文件中

[address]w filename

地址可以是单行,多行,或一个文本匹配

sed '1,3w test' fstatt.cpp

(8)从一个文件中读

[address]r filename

    You can't use a range of addresses for the read command. You can only specify a single line number or text pattern address. The sed editor inserts the text from the file after the address.

sed '3r data' data2

    The sed editor inserts the complete text from the data file into the data2 file, starting at line 3 of the data2 file.

(9)字元的替换(Transform command

    The transform command (y) is the only sed editor command that operates on a single character.it performs a one-to-one mapping of the inchars and the outchars values.If the inchars and outchars are not the same length, the sed editor will produce an error message.

[address]y/inchars/outchars/

(10)反相执行命令

[address1[ , address2]] !函数参数

sed '/asd/!d' test

(11)读入下一行资料: n

[address1[ ,address2] n

sed -n -e '/echo/n' -e 'p' temp //输出文件,但如果一行含有字符串echo,则输出包含该字符串的下一行。

(12) 命令的复用

    一次执行多个命令的方式有三种:

sed 's/w1/w2/g; 1/i\words' filename(使用;号把命令隔开,前面不加-e)

sed -e 'cmd1' -e 'cmd2'  filename(使用多个-e参数)

3、注意事项

1)$为最后一行,如果要查询包含$的行,要进行转义:

sed -n '/\$/p' test

4、more in document

如图中所示【4】,我们通常便在pattern space模式下。

参考

1http://www.cnblogs.com/mydomain/archive/2010/10/14/1851755.html

3http://blog.microsuncn.com/?p=1118

3document

【4】 http://www.cnblogs.com/mydomain/gallery/image/107992.html

抱歉!评论已关闭.