首页 > 代码库 > sed(转)

sed(转)

第一部分:sed基础

1)简介
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写文本转换程序等。掌握好sed的用法,能够极大的提高我们各类文件处理工作的效率。
 
2)sed命令参数及选项
 命令 功能
 a\ 在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行
 c\ 用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用”\”续行
 i\ 在当前行之前插入文本。多行时除最后一行外,每行末尾需用”\”续行
 d 删除行
 h 把模式空间里的内容复制到暂存缓冲区
 H 把模式空间里的内容追加到暂存缓冲区
 g 把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容
 G 把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面
 l 列出非打印字符
 p 打印行
 n 读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理
 q 结束或退出sed
 r 从文件中读取输入行
 ! 对所选行以外的所有行应用命令
 s 用一个字符串替换另一个
 g 在行内进行全局替换
 w 将所选的行写入文件
 x 交换暂存缓冲区与模式空间的内容
 y 将字符替换为另一字符(不能对正则表达式使用y命令)
选项 功能
 -e 进行多项编辑,即对输入行应用多条sed命令时使用
 -n 取消默认的输出
 -f 指定sed脚本的文件名

3)正则规则

身为一个强大的文本处理工具,支持正则表达式是必须的。sed使用的正则表达式是括在斜杠线”/”之间的模式。
元字符 功能 示例
 ^ 行首定位符 /^my/  匹配所有以my开头的行
 $ 行尾定位符 /my$/  匹配所有以my结尾的行
 . 匹配除换行符以外的单个字符 /m..y/  匹配包含字母m,后跟两个任意字符,再跟字母y的行
 * 匹配零个或多个前导字符 /my*/  匹配包含字母m,后跟零个或多个y字母的行
 [] 匹配指定字符组内的任一字符 /[Mm]y/  匹配包含My或my的行
 [^] 匹配不在指定字符组内的任一字符 /[^Mm]y/  匹配包含y,但y之前的那个字符不是M或m的行
 \(..\) 保存已匹配的字符 1,20s/\(you\)self/\1r/  标记元字符之间的模式,并将其保存为标签1,之后可以使用\1来引用它。最多可以定义9个标签,从左边开始编号,最左边的是第一个。此例中,对第1到第20行进行处理,you被保存为标签1,如果发现youself,则替换为your。
 & 保存查找串以便在替换串中引用 s/my/**&**/  符号&代表查找串。my将被替换为**my**
 \< 词首定位符 /\<my/  匹配包含以my开头的单词的行
 \> 词尾定位符 /my\>/  匹配包含以my结尾的单词的行
 x\{m\} 连续m个x /9\{5\}/ 匹配包含连续5个9的行
 x\{m,\} 至少m个x /9\{5,\}/  匹配包含至少连续5个9的行
 x\{m,n\} 至少m个,但不超过n个x /9\{5,7\}/  匹配包含连续5到7个9的行
 
4)10个小实例
sed命令的参数选项比较多,我们直接通过一些实例来了解sed的用法。
 
实例1.p命令
p命令用于输出文件内容,对于匹配模式的行,会被输出两次。
例如:
 
1
2
3
4
5
6
7
8
9
10
11
cricode>>cat sed.txt
#This is a sed command test file
Hello,world hello,world
if linux command www.cricode.com
welcome to cricode
cricode>>sed ‘/Hello/p‘ sed.txt
#This is a sed command test file
Hello,world hello,world
Hello,world hello,world
if linux command www.cricode.com
welcome to cricode

文件中包含Hello的行被输出了两次。

 

如果只想要输出匹配模式的行,则可以通过-n参数取消sed的默认行为来实现。
 
1
2
cricode>>sed -n ‘/Hello/p‘ sed.txt
Hello,world hello,world

实例2. d命令

 

d命令用于删除特定行。
删除第1、2行
 
1
2
3
cricode>>sed ‘1,2d‘ sed.txt
if linux command www.cricode.com
welcome to cricode

删除最后一行

 

 
1
2
3
4
cricode>>sed ‘$d‘ sed.txt
#This is a sed command test file
Hello,world hello,world
if linux command www.cricode.com

删除包含字符串cricode的行

 

 
1
2
3
cricode>>sed ‘/cricode/d‘ sed.txt
#This is a sed command test file
Hello,world hello,world

删除以com字符串结尾的行

 

 
1
2
3
4
cricode>>sed ‘/com$/d‘ sed.txt
#This is a sed command test file
Hello,world hello,world
welcome to cricode

实例3. S命令

 

s命令用于文本替换。
将文件中world替换成WORLD
 
1
2
3
4
5
cricode>>sed ‘s/world/WORLD/‘ sed.txt
#This is a sed command test file
Hello,WORLD hello,world
if linux command www.cricode.com
welcome to cricode

可以发现Hello,WORLD hello,world 中只有第一个world被替换了。

 

 通过g参数在全行进行替换,如下:
 
1
2
3
4
5
cricode>>sed ‘s/world/WORLD/g‘ sed.txt
#This is a sed command test file
Hello,WORLD hello,WORLD
if linux command www.cricode.com
welcome to cricode

整行替换,将全文中包含特定单词的行替换成其他字符串。

 

 
1
2
3
4
5
cricode>>sed ‘s/.*world/this is a replace line/‘ sed.txt
#This is a sed command test file
this is a replace line
if linux command www.cricode.com
welcome to cricode

实例4. e选项

 

-e即edit的意思,编辑命令,用于sed执行多个编辑任务的情况下。在下一行开始编辑前,所有的编辑动作将应用到模式缓冲区中的行上。
 
1
2
3
cricode>>sed -e ‘1,2d‘ -e ‘s/cricode/CRICODE/‘ sed.txt
if linux command www.CRICODE.com
welcome to CRICODE

上述为sed命令使用选项-e进行多重编辑后得到的结果。第一重编辑删除第1、2行。第二重编辑将第1、2行外的所有包含cricode替换为大写CRICODE。因为是逐行进行这两项编辑(即这两个命令都在模式空间的当前行上执行),所以编辑命令的顺序会影响结果。

 

实例5.r命令
r命令是读命令。sed使用该命令将一个文本文件中的内容加到当前文件的特定位置上。这个命令在需要往文件中有规律的插入信息时特别有用。
在包含cricode单词的行后面加入一行注释。(注释内容写在sed1.txt中)
 
1
2
3
4
5
6
7
cricode>>sed ‘/cricode/r sed1.txt‘ sed.txt
#This is a sed command test file
Hello,world hello,world
if linux command www.cricode.com
###This is comment information!!!!!!!!!!!!!
welcome to cricode
###This is comment information!!!!!!!!!!!!!

实例6. w命令

 

 
1
2
3
4
5
6
7
8
cricode>>sed ‘/cricode/w tmp.txt‘ sed.txt
#This is a sed command test file
Hello,world hello,world
if linux command www.cricode.com
welcome to cricode
cricode>>cat tmp.txt
if linux command www.cricode.com
welcome to cricode

上述命令将sed.txt文件中包含模式cricode的行内容写入到tmp.txt

 

实例7.a\命令

a\命令将添加新文本到文件中当前行(即读入模式缓冲区中的行)的后面。所追加的文本行位于sed命令的下方另起一行。如果要追加的内容超过一行,则每一行都必须以反斜线结束,最后一行除外。最后一行将以引号和文件名结束。

在包含hello的行后插入一行内容:kidding??

 
1
2
3
4
5
6
cricode>>sed ‘/hello/a\kidding???‘ sed.txt
#This is a sed command test file
Hello,world hello,world
kidding???
if linux command www.cricode.com
welcome to cricode

其实第5点的r命令也能实现相同的功能。

实例8.i\命令

i\命令与a\命令相反,它是在匹配行的前面插入一行。

 在包含hello的行前插入一行内容:kidding??

 
1
2
3
4
5
6
7
cricode>>sed ‘/hello/i\kidding???‘ sed.txt
 
#This is a sed command test file
kidding???
Hello,world hello,world
if linux command www.cricode.com
welcome to cricode

实例9. c\命令

c\命令将匹配行修改成我们设定的内容。

将包含hello的行替换成kidding??

 
1
2
3
4
5
cricode>>sed ‘/hello/c\kidding???‘ sed.txt
#This is a sed command test file
kidding???
if linux command www.cricode.com
welcome to cricode

其实这个替换功能可以通过第3点中方法实现:通过正则匹配来实现全行替换。

实例10. y命令

y命令与UNIX/Linux中的tr命令类似,字符按照一对一的方式从左到右进行转换。例如,y/abc/ABC/将把所有小写的a转换成A,小写的b转换成B,小写的c转换成C。

将文本中小写字母hi分别替换为大写字母HI:

 
1
2
3
4
5
cricode>>sed ‘y/hi/HI/‘ sed.txt
#THIs Is a sed command test fIle
Hello,world Hello,world
If lInux command www.crIcode.com
welcome to crIcode

 

 第二部分:Sed脚本

通过编写脚本我们可以方便的批量执行命令。sed脚本就是写在文件中的一系列sed命令。sed脚本中,要求命令的末尾不能有任何多余的空格或文本。如果在一行中有多个命令,要用分号分隔。执行脚本 时,sed先将输入文件中第一行复制到模式缓冲区,然后对其执行脚本中所有的命令。每一行处理完毕后,sed再复制文件中下一行到模式缓冲区,对其执行脚 本中所有命令。使用sed脚本时,不再用引号来确保sed命令不被shell解释。

下面举个栗子。

我们现在来完成如下任务:

1)在sed.txt中的开头插入一行作者信息:#Author:Jay13  2013-08-25

2)将文章中所有cricode替换成CRICODE。

3)在文本末尾加入一行:good bye!

那么我们可以编写一个名为sedscript脚本,脚本具体内容如下:

 
1
2
3
1i\#Author:Jay13 2013-08-25
s/cricode/CRICODE/g
$a\good bye!

然后再将sed脚本应用到sed.txt 文件上

 
1
2
3
4
5
6
7
cricode>>sed -f sedscript sed.txt
#Author:Jay13 2013-08-25
#This is a sed command test file
Hello,world hello,world
if linux command www.CRICODE.com
welcome to CRICODE
good bye!

 

第三部分:练习

学习了sed的基本知识,现在我们来实战一下。

1、Sed中如何替换多行中的有规律的数字字符串 

输入:

hello world balabala – . gene_id “240838 “; transcript_id “240838“;

hello world balabala again – . gene_id “240838 “; transcript_id “240838“;

balaba….

输出:

hello world balabala - . gene_id “zgg240838 “; transcript_id “zgg240838“;

hello world balabala again - . gene_id “zgg240838 “; transcript_id “zgg240838“;

balaba…

也即,将字符串gene_id“后接数字以及transcript_id “后接数字中的引号与数字之间插入zgg字符串。

解答:

脚本编写如下:

sed  ‘s/gene_id “\([0-9]\+\)”; transcript_id “\([0-9]\+\)”;/gene_id “zgg\1″; transcript_id “zgg\2″;/g’ test.txt

注:

1. 数字的匹配[0-9]

2.[0-9]后接\+表示匹配一个或多个数字

3.匹配部分加括号,引用匹配部分用\1,\2,….等等。

4.sed执行多个匹配用分号连接,整个命令用’’引在内部。

2、在文本中每一行添加行头行尾,在行投添加 Head单词,在行尾添加Tail单词。

解答:略

3、利用sed进行格式转换

比如需要将text.txt中如下内容

 
1
2
3
4
5
6
7
8
dn: uid=admin,ou=ITaccounts,dc=tc
uid: admin
cn: admin cn
sn: admin sn
dn: uid=0037,ou=employees,dc=tci
uid: 0037
cn: thinker
sn: zzz

转换成xml格式如下:

 
1
2
3
4
5
6
7
8
9
10
11
12
<entity>
<dn>uid=admin,ou=ITaccounts,dc=tc</dn>
<uid>admin</uid>
<cn>admin cn</cn>
<sn>admin sn</sn>
</entity>
<entity>
<dn>uid=0037,ou=employees,dc=tci</dn>
<uid>0037</uid>
<cn>thinker</cn>
<sn>zzz</sn>
</entity>

则可以定义下面的sedscript文件:

 
1
2
3
4
5
6
7
/^dn:[[:space:]]/i
<entity
s/^dn:[[:space:]]\(.*\)$/<dn>\1<\/dn>/
s/^uid:[[:space:]]\(.*\)$/<uid>\1<\/uid>/
s/^cn:[[:space:]]\(.*\)$/<cn>\1<\/cn>/
s/^sn:[[:space:]]\(.*\)$/<sn>\1<\/sn>/
s/^$/<\/entity>/g

第一行和第二行实现的功能就是在每个dn:开头的行前面加上一行<entity>。最后一行是碰到空行补上</entity>这 个结束标记。

另外,在执行替换的时候可以通过使用把被替换的字符串分组,在替换的部分里面用\1、\2这种方式引用被替换文字里面的相应的组。

用这种方法把ldif文件里面[:]前的东西删掉并且在两边增加相应的xml标记。
仔细想想,4行替换也许用下面的1行就能实现?
s/^\(.*\):[[:space:]]\(.*\)$/<\1>\2<\/\1>/g

 
1
2
3
4
5
6
7
8
9
10
11
cricode>> sed -f sedscript test.txt
<entity>
<dn>uid=admin,ou=ITaccounts,dc=tc</dn>
<uid>admin</uid>
<cn>admin cn</cn>
<sn>admin sn</sn>
<entity>
<dn>uid=0037,ou=employees,dc=tci</dn>
<uid>0037</uid>
<cn>thinker</cn>
<sn>zzz</sn>

=============================GAME OVER================================

参考:http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html
http://www.cricode.com/3364.html

sed(转)