sed

Sed

一 sed简介

sed是stream editor的缩写,一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

1.1 sed命令格式

sed [nefri] 'command' file(s)

常用选项:

-n  使用安静(silent)模式。在一般sed的用法中,所有来自stdin的资料一般都会被列出到屏幕,但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者command)才会被列出来。
-e  允许多点编辑。
-f  直接将sed的动作写在一个档案内,-f filename 则可以执行filename内的sed动作。
-r  sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i  直接修改读取的档案内容,而不是由屏幕输出。

常用command:

a\  新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c\  取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d  删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i\  插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p  列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s  取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

高级command:

命令

功能描述

h

 拷贝pattern space的内容到holding buffer(特殊缓冲区)。

H

 追加pattern space的内容到holding buffer。

g

 获得holding buffer中的内容,并替代当前pattern space中的文本。

G

 获得holding buffer中的内容,并追加到当前pattern space的后面。

n

 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。

P

 打印pattern space中的第一行。  //大写

q

 退出sed。

w file

 写并追加pattern space到file的末尾。

!

 表示后面的命令对所有没有被选定的行发生作用。

s/re/string

 用string替换正则表达式re。

=

 打印当前行号码。

替换标记

 

g

 行内全面替换,如果没有g,只替换第一个匹配。

x

 互换pattern space和holding buffer中的文本。

y

 把一个字符翻译为另一个字符(但是不能用于正则表达式)。

需要说明的是,sed中的正则和grep的基本相同,完全可以参照本系列的第一篇中的详细说明。

1.2 元字符集

^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。 

$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。 

. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。 

* 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。 

[] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。

 [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。

 ....able/\1rs,loveable被替换成lovers。 

& 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**。 

\< 匹配单词的开始,如:/\ 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行。 

x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行。 

x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行。 

x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行。

二 sed实例

2.1 初级:

假设我们有一文件名为ab,
# sed '1d' ab             #删除第一行 
# sed '$d' ab             #删除最后一行
# sed '1,2d' ab           #删除第一行到第二行
# sed '2,$d' ab           #删除第二行到最后一行
# sed -n '1p' ab           #显示第一行 
# sed -n '$p' ab           #显示最后一行
# sed -n '1,2p' ab        #显示第一行到第二行
# sed -n '2,$p' ab        #显示第二行到最后一行
# sed -n '/ruby/p' ab    #查询包括关键字ruby所在所有行
# sed -n '/\$/p' ab        #查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义


# cat ab
Hello!
ruby is me,welcome to my blog.
end
# sed '1a drink tea' ab  #第一行后增加字符串"drink tea"
Hello!
drink tea
ruby is me,welcome to my blog. 
end
# sed '1,3a drink tea' ab #第一行到第三行后增加字符串"drink tea"
Hello!
drink tea
ruby is me,welcome to my blog.
drink tea
end
drink tea
# sed '1a drink tea\nor coffee' ab   #第一行后增加多行,使用换行符\n
Hello!
drink tea
or coffee
ruby is me,welcome to my blog.
end


# sed '1c Hi' ab                #第一行代替为Hi
Hi
ruby is me,welcome to my blog.
end
# sed '1,2c Hi' ab             #第一行到第二行代替为Hi
Hi
end


格式:sed 's/要替换的字符串/新的字符串/g'   (要替换的字符串可以用正则表达式)
# sed -n '/ruby/p' ab | sed 's/ruby/bird/g'    #替换ruby为bird
# sed -n '/ruby/p' ab | sed 's/ruby//g'        #删除ruby

# sed -i '$a bye' ab         #在文件ab中最后一行直接输入"bye"
# cat ab
Hello!
ruby is me,welcome to my blog.
end
bye

sed -i '/匹配字符串/d'  filename  (注:若匹配字符串是变量,则需要“”,而不是‘’。记得好像是)

sed -i '/匹配字符串/s/替换源字符串/替换目标字符串/g' filename

2.2 中级

# cat testfile

northwest       NW     Charles Main         3.0     .98      3       34

western         WE     Sharon Gray          5.3     .97      5       23

southwest       SW     Lewis Dalsass        2.7     .8       2       18

southern        SO     Suan Chin            5.1     .95      4       15

southeast       SE     Patricia Hemenway    4.0     .7       4       17

eastern         EA     TB Savage            4.4     .84      5       20

northeast       NE     AM Main Jr.          5.1     .94      3       13

north           NO     Margot Weber         4.5     .89      5       9

central         CT     Ann Stephens         5.7     .94      5       13

如果模板north被找到,sed除了打印所有行之外,还有打印匹配行。

# sed '/north/p' testfile

northwest       NW     Charles Main         3.0     .98     3       34

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

-n选项取消了sed的默认行为。在没有-n的时候,包含模板的行被打印两次,但是在使用-n的时候将只打印包含模板的行。

# sed -n '/north/p' testfile 

northwest       NW     Charles Main         3.0     .98     3       34

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

列出第5-7行

# nl testfile |sed -n '5,7p'

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

删除第三行,其他行默认输出到屏幕。

# nl testfile |sed '3d'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     2  western         WE     Sharon Gray          5.3     .97     5       23

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

删除2~5行

# nl testfile |sed '2,5d'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

从第三行删除到最后一行,其他行被打印。$表示最后一行。

# nl testfile |sed '3,$d'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     2  western         WE     Sharon Gray          5.3     .97     5       23

删除最后一行,其他行打印。

# nl testfile |sed '$d'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     2  western         WE     Sharon Gray          5.3     .97     5       23

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

删除所有包含north的行,其他行打印。

# nl testfile |sed '/north/d' 

     2  western         WE     Sharon Gray          5.3     .97     5       23

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     9  central         CT     Ann Stephens         5.7     .94     5       13

在第二行后(即加在第三行)加上"United States"。

# nl testfile |sed '2a United States'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     2  western         WE     Sharon Gray          5.3     .97     5       23

United States

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

如果要在第二行前加,则命令为

# nl testfile |sed '2i United States'

在第二行后加上两行文本。

# nl testfile |sed '2a United States \
> America'

     1  northwest       NW     Charles Main         3.0     .98     3       34

     2  western         WE     Sharon Gray          5.3     .97     5       23

United States 

America

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

若要新增两行,则每一行之间都必须要以反斜线“\”来进行新行的增加。

将第2~5行的内容取代为“No 2-5 number”。

# nl testfile |sed '2,5c No 2-5 number'

     1  northwest       NW     Charles Main         3.0     .98     3       34

No 2-5 number

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

s表示替换,g表示命令作用于整个当前行。如果该行存在多个west,都将被替换为north,如果没有g,则只是替换第一个匹配。

格式:

Sed

一 sed简介

sed是stream editor的缩写,一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

1.1 sed命令格式

sed [nefri] 'command' file(s)

常用选项:

-n  使用安静(silent)模式。在一般sed的用法中,所有来自stdin的资料一般都会被列出到屏幕,但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者command)才会被列出来。
-e  允许多点编辑。
-f  直接将sed的动作写在一个档案内,-f filename 则可以执行filename内的sed动作。
-r  sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i  直接修改读取的档案内容,而不是由屏幕输出。

常用command:

a\  新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c\  取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d  删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i\  插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p  列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s  取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

高级command:

命令

功能描述

h

 拷贝pattern space的内容到holding buffer(特殊缓冲区)。

H

 追加pattern space的内容到holding buffer。

g

 获得holding buffer中的内容,并替代当前pattern space中的文本。

G

 获得holding buffer中的内容,并追加到当前pattern space的后面。

n

 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。

P

 打印pattern space中的第一行。  //大写

q

 退出sed。

w file

 写并追加pattern space到file的末尾。

!

 表示后面的命令对所有没有被选定的行发生作用。

s/re/string

 用string替换正则表达式re。

=

 打印当前行号码。

替换标记

 

g

 行内全面替换,如果没有g,只替换第一个匹配。

x

 互换pattern space和holding buffer中的文本。

y

 把一个字符翻译为另一个字符(但是不能用于正则表达式)。

需要说明的是,sed中的正则和grep的基本相同,完全可以参照本系列的第一篇中的详细说明。

1.2 元字符集

^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。 

$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。 

. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。 

* 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。 

[] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。

 [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。

 ....got/\1lianne/p'

     8  north           NO     Marlianne Weber         4.5     .89     5       9

s后面的字符一定是分隔搜索字符串和替换字符串的分隔符,默认为斜杠,但是在s命令使用的情况下可以改变。不论什么字符紧跟着s命令都认为是新的分隔符。这个技术在搜索含斜杠的模板时非常有用,例如搜索时间和路径的时候。

# sed 's#3#88#g' testfile

northwest       NW     Charles Main         88.0     .98     88       884

western         WE     Sharon Gray          5.88     .97     5       288

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     88       188

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       188

# sed 's@3@88@g' testfile

northwest       NW     Charles Main         88.0     .98     88       884

western         WE     Sharon Gray          5.88     .97     5       288

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     88       188

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       188

所有在模板west和east所确定的范围内的行都被打印,如果west出现在east后面的行中,从west开始到下一个east,无论这个 east出现在哪里,二者之间的行都被打印,即使从west开始到文件的末尾还没有出现east,那么从west到末尾的所有行都将打印。

# nl testfile |sed -n '/south/,/east/p'

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

打印从第五行开始到第一个以northeast开头的行之间的所有行。

# sed -n '5,/^northeast/p' testfile

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

-e选项表示多点编辑。第一个编辑命令是删除第一到第三行。第二个编辑命令是用Jones替换Hemenway。

# nl testfile |sed -e '1,3d' -e 's/Hemenway/Jones/'

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Jones    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

将所有匹配含有north的行写入newfile中。

# sed -n '/north/w newfile' testfile

# cat newfile

northwest       NW     Charles Main         3.0     .98     3       34

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

i是插入命令,在匹配模式行前插入文本。

# sed '/eastern/i\NEW ENGLAND REGION' testfile 

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

NEW ENGLAND REGION

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

找到匹配模式eastern的行后,执行后面花括号中的一组命令,每个命令之间用逗号分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替换,p和-n选项的合用,则只是打印作用到的行。

# sed -n '/eastern/{n;s/AM/Archie/;p}' testfile

northeast       NE     Archie Main Jr.          5.1     .94     3       13

-e表示多点编辑,第一个编辑命令y将前三行中的所有小写字母替换为大写字母,-n表示不显示替换后的输出,第二个编辑命令将只是打印输出转换后的前三行。注意y不能用于正则。

# sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile

NORTHWEST       NW     CHARLES MAIN         3.0     .98     3       34

WESTERN         WE     SHARON GRAY          5.3     .97     5       23

SOUTHWEST       SW     LEWIS DALSASS        2.7     .8      2       18

打印完第二行后退出。

# sed '2q' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

当模板Lewis在某一行被匹配,替换命令首先将Lewis替换为Joseph,然后再用q退出sed。

# sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Joseph Dalsass        2.7     .8      2       18

在sed处理文件的时候,每一行都被保存在pattern space的临时缓冲区中。除非行被删除或者输出被取消,否则所有被处理过的行都将打印在屏幕上。接着pattern space被清空,并存入新的一行等待处理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令将其复制并存入一个称为holding buffer的特殊缓冲区内。在第二个sed编辑命令中,当达到最后一行后,G命令告诉sed从holding buffer中取得该行,然后把它放回到pattern space中,且追加到现在已经存在于模式空间的行的末尾。

# sed -e '/northeast/h' -e '$G' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

northeast       NE     AM Main Jr.          5.1     .94     3       13

如果模板WE在某一行被匹配,h命令将使得该行从pattern space中复制到holding buffer中,d命令再将该行删除,因此WE匹配行没有在原来的位置被输出。第二个命令搜索CT,一旦被找到,G命令将从holding buffer中取回行,并追加到当前pattern space的行末尾。简单的说,WE所在的行被移动并追加到包含CT行的后面。

# sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

western         WE     Sharon Gray          5.3     .97     5       23

第一个命令将匹配northeast的行从pattern space复制到holding buffer,第二个命令在读取的文件的末尾时,g命令告诉sed从holding buffer中取得行,并把它放回到pattern space中,以替换已经存在于pattern space中的。简单说就是包含模板northeast的行被复制并覆盖了文件的末尾行。

# sed -e '/northeast/h' -e '$g' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

northeast       NE     AM Main Jr.          5.1     .94     3       13

模板WE匹配的行被h命令复制到holding buffer,再被d命令删除。结果可以看出WE的原有位置没有输出。第二个编辑命令将找到匹配CT的行,g命令将取得holding buffer中的行,并覆盖当前pattern space中的行,即匹配CT的行。简单的说,任何包含模板northeast的行都将被复制,并覆盖包含CT的行。

# sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

western         WE     Sharon Gray          5.3     .97     5       23

第一个编辑中的h命令将匹配Patricia的行复制到holding buffer中,第二个编辑中的x命令,会将holding buffer中的文本考虑到pattern space中,而pattern space中的文本被复制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一个命令中匹配Patricia的行文本,第三个编辑命令会将交互后的holding buffer中的文本在最后一行的后面打印出来。

# sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

southeast       SE     Patricia Hemenway    4.0     .7      4       17

central       &nb

Sed

一 sed简介

sed是stream editor的缩写,一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

1.1 sed命令格式

sed [nefri] 'command' file(s)

常用选项:

-n  使用安静(silent)模式。在一般sed的用法中,所有来自stdin的资料一般都会被列出到屏幕,但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者command)才会被列出来。
-e  允许多点编辑。
-f  直接将sed的动作写在一个档案内,-f filename 则可以执行filename内的sed动作。
-r  sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i  直接修改读取的档案内容,而不是由屏幕输出。

常用command:

a\  新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c\  取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d  删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i\  插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p  列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s  取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

高级command:

命令

功能描述

h

 拷贝pattern space的内容到holding buffer(特殊缓冲区)。

H

 追加pattern space的内容到holding buffer。

g

 获得holding buffer中的内容,并替代当前pattern space中的文本。

G

 获得holding buffer中的内容,并追加到当前pattern space的后面。

n

 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。

P

 打印pattern space中的第一行。  //大写

q

 退出sed。

w file

 写并追加pattern space到file的末尾。

!

 表示后面的命令对所有没有被选定的行发生作用。

s/re/string

 用string替换正则表达式re。

=

 打印当前行号码。

替换标记

 

g

 行内全面替换,如果没有g,只替换第一个匹配。

x

 互换pattern space和holding buffer中的文本。

y

 把一个字符翻译为另一个字符(但是不能用于正则表达式)。

需要说明的是,sed中的正则和grep的基本相同,完全可以参照本系列的第一篇中的详细说明。

1.2 元字符集

^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。 

$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。 

. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。 

* 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。 

[] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。

 [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。

 ....got/\1lianne/p'

     8  north           NO     Marlianne Weber         4.5     .89     5       9

s后面的字符一定是分隔搜索字符串和替换字符串的分隔符,默认为斜杠,但是在s命令使用的情况下可以改变。不论什么字符紧跟着s命令都认为是新的分隔符。这个技术在搜索含斜杠的模板时非常有用,例如搜索时间和路径的时候。

# sed 's#3#88#g' testfile

northwest       NW     Charles Main         88.0     .98     88       884

western         WE     Sharon Gray          5.88     .97     5       288

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     88       188

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       188

# sed 's@3@88@g' testfile

northwest       NW     Charles Main         88.0     .98     88       884

western         WE     Sharon Gray          5.88     .97     5       288

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     88       188

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       188

所有在模板west和east所确定的范围内的行都被打印,如果west出现在east后面的行中,从west开始到下一个east,无论这个 east出现在哪里,二者之间的行都被打印,即使从west开始到文件的末尾还没有出现east,那么从west到末尾的所有行都将打印。

# nl testfile |sed -n '/south/,/east/p'

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

打印从第五行开始到第一个以northeast开头的行之间的所有行。

# sed -n '5,/^northeast/p' testfile

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

-e选项表示多点编辑。第一个编辑命令是删除第一到第三行。第二个编辑命令是用Jones替换Hemenway。

# nl testfile |sed -e '1,3d' -e 's/Hemenway/Jones/'

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Jones    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

将所有匹配含有north的行写入newfile中。

# sed -n '/north/w newfile' testfile

# cat newfile

northwest       NW     Charles Main         3.0     .98     3       34

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

i是插入命令,在匹配模式行前插入文本。

# sed '/eastern/i\NEW ENGLAND REGION' testfile 

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

NEW ENGLAND REGION

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

找到匹配模式eastern的行后,执行后面花括号中的一组命令,每个命令之间用逗号分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替换,p和-n选项的合用,则只是打印作用到的行。

# sed -n '/eastern/{n;s/AM/Archie/;p}' testfile

northeast       NE     Archie Main Jr.          5.1     .94     3       13

-e表示多点编辑,第一个编辑命令y将前三行中的所有小写字母替换为大写字母,-n表示不显示替换后的输出,第二个编辑命令将只是打印输出转换后的前三行。注意y不能用于正则。

# sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile

NORTHWEST       NW     CHARLES MAIN         3.0     .98     3       34

WESTERN         WE     SHARON GRAY          5.3     .97     5       23

SOUTHWEST       SW     LEWIS DALSASS        2.7     .8      2       18

打印完第二行后退出。

# sed '2q' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

当模板Lewis在某一行被匹配,替换命令首先将Lewis替换为Joseph,然后再用q退出sed。

# sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Joseph Dalsass        2.7     .8      2       18

在sed处理文件的时候,每一行都被保存在pattern space的临时缓冲区中。除非行被删除或者输出被取消,否则所有被处理过的行都将打印在屏幕上。接着pattern space被清空,并存入新的一行等待处理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令将其复制并存入一个称为holding buffer的特殊缓冲区内。在第二个sed编辑命令中,当达到最后一行后,G命令告诉sed从holding buffer中取得该行,然后把它放回到pattern space中,且追加到现在已经存在于模式空间的行的末尾。

# sed -e '/northeast/h' -e '$G' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

northeast       NE     AM Main Jr.          5.1     .94     3       13

如果模板WE在某一行被匹配,h命令将使得该行从pattern space中复制到holding buffer中,d命令再将该行删除,因此WE匹配行没有在原来的位置被输出。第二个命令搜索CT,一旦被找到,G命令将从holding buffer中取回行,并追加到当前pattern space的行末尾。简单的说,WE所在的行被移动并追加到包含CT行的后面。

# sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

central         CT     Ann Stephens         5.7     .94     5       13

western         WE     Sharon Gray          5.3     .97     5       23

第一个命令将匹配northeast的行从pattern space复制到holding buffer,第二个命令在读取的文件的末尾时,g命令告诉sed从holding buffer中取得行,并把它放回到pattern space中,以替换已经存在于pattern space中的。简单说就是包含模板northeast的行被复制并覆盖了文件的末尾行。

# sed -e '/northeast/h' -e '$g' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

northeast       NE     AM Main Jr.          5.1     .94     3       13

模板WE匹配的行被h命令复制到holding buffer,再被d命令删除。结果可以看出WE的原有位置没有输出。第二个编辑命令将找到匹配CT的行,g命令将取得holding buffer中的行,并覆盖当前pattern space中的行,即匹配CT的行。简单的说,任何包含模板northeast的行都将被复制,并覆盖包含CT的行。

# sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile

northwest       NW     Charles Main         3.0     .98     3       34

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

north           NO     Margot Weber         4.5     .89     5       9

western         WE     Sharon Gray          5.3     .97     5       23

第一个编辑中的h命令将匹配Patricia的行复制到holding buffer中,第二个编辑中的x命令,会将holding buffer中的文本考虑到pattern space中,而pattern space中的文本被复制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一个命令中匹配Patricia的行文本,第三个编辑命令会将交互后的holding buffer中的文本在最后一行的后面打印出来。

# sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile

northwest       NW     Charles Main         3.0     .98     3       34

western         WE     Sharon Gray          5.3     .97     5       23

southwest       SW     Lewis Dalsass        2.7     .8      2       18

southern        SO     Suan Chin            5.1     .95     4       15

southeast       SE     Patricia Hemenway    4.0     .7      4       17

eastern         EA     TB Savage            4.4     .84     5       20

northeast       NE     AM Main Jr.          5.1     .94     3       13

southeast       SE     Patricia Hemenway    4.0     .7      4       17

central         CT     Ann Stephens         5.7     .94     5       13

north           NO     Margot Weber         4.5     .89     5       9

 参考:http://www.cnblogs.com/mchina/archive/2012/06/30/2571308.html

 

sp; CT     Ann Stephens         5.7     .94     5       13

north           NO     Margot Weber         4.5     .89     5       9

 参考:http://www.cnblogs.com/mchina/archive/2012/06/30/2571308.html

 

1,51);">sed 's/要替换的字符串/新的字符串/g'

# nl testfile |sed 's/west/north/g'

     1  northnorth       NW     Charles Main         3.0     .98     3       34

     2  northern         WE     Sharon Gray          5.3     .97     5       23

     3  southnorth       SW     Lewis Dalsass        2.7     .8      2       18

     4  southern        SO     Suan Chin            5.1     .95     4       15

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17

     6  eastern         EA     TB Savage            4.4     .84     5       20

     7  northeast       NE     AM Main Jr.          5.1     .94     3       13

     8  north           NO     Margot Weber         4.5     .89     5       9

     9  central         CT     Ann Stephens         5.7     .94     5       13

-n表示只打印匹配行,如果某一行的开头是west,则替换为north。

# sed -n 's/^west/north/p' testfile 

northern         WE     Sharon Gray          5.3     .97     5       23

&符号表示替换字符串中被找到的部分。所有以两个数字结束的行,最后的数字都将被它们自己替换,同时追加.5。

# nl testfile |sed 's/[0-9][0-9]$/&.5/'

     1  northwest       NW     Charles Main         3.0     .98     3       34.5

     2  western         WE     Sharon Gray          5.3     .97     5       23.5

     3  southwest       SW     Lewis Dalsass        2.7     .8      2       18.5

     4  southern        SO     Suan Chin            5.1     .95     4       15.5

     5  southeast       SE     Patricia Hemenway    4.0     .7      4       17.5

     6  eastern         EA&

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值