鉴于前面有篇博文里面提到了sed用法,有几个朋友也问了些sed的相关的问题。其中我发现都是一些sed的基本用法,那我想还是把我的sed的用法总结发出来,给大家看看。希望对大家有所帮助。也算是让我复习了一遍吧~~~ 嘻嘻 ^_^

------------------------------------------------------------------------------------------------------

1. sed简介

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

2. 定址

可以通过定址来定位你所希望编辑的行,该地址用数字构成,用逗号分隔的两个行数表示以这两行为起止的行的范围(包括行数表示的那两行)。如13表示123行,美元符号($)表示最后一行。范围可以通过数据,正则表达式或者二者结合的方式确定 。

3. sed命令

调用sed命令有两种形式:

sed [options] 'command' file(s)

sed [options] -f scriptfile file(s)

  • a\

    在当前行后面加入一行文本。

  • b lable

    分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾。

  • c\

    用新的文本改变本行的文本。

  • d

    从模板块(Pattern space)位置删除行。

  • D

    删除模板块的第一行。

  • i\

    在当前行上面插入文本。

  • h

    拷贝模板块的内容到内存中的缓冲区。

  • H

    追加模板块的内容到内存中的缓冲区

  • g

    获得内存缓冲区的内容,并替代当前模板块中的文本。

  • G

    获得内存缓冲区的内容,并追加到当前模板块文本的后面。

  • l

    列表不能打印字符的清单。

  • n

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

  • N

    追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。

  • p

    打印模板块的行。

  • P(大写)

    打印模板块的第一行。

  • q

    退出sed

  • r file

    file中读行。

  • t label

    if分支,从最后一行开始,条件一旦满足或者Tt命令,将导致分支到带有标号的命令处,或者到脚本的末尾。

  • T label

    错误分支,从最后一行开始,一旦发生错误或者Tt命令,将导致分支到带有标号的命令处,或者到脚本的末尾。

  • w file

    写并追加模板块到file末尾。

  • W file

    写并追加模板块的第一行到file末尾。

  • !

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

  • s/re/string

    string替换正则表达式re

  • =

    打印当前行号码。

  • #

    把注释扩展到下一个换行符以前。

以下的是替换标记

  • g表示行内全面替换。

  • p表示打印行。

  • w表示把行写入一个文件。

  • x表示互换模板块中的文本和缓冲区中的文本。

  • y表示把一个字符翻译为另外的字符(但是不用于正则表达式)

PS:如果你和我一样也是个新手的话,那么以上×××的部分是你需要掌握的哦!】

4. 选项

  • -e command, --command< /span>

    允许多台编辑。

  • -h, --help

    打印帮助,并显示bug列表的地址。

  • -n, --quiet, --silent

    取消默认输出。

  • -f, --filer=script-file

    引导sed脚本文件名。

  • -V, --version

    打印版本和版权信息。

5. 元字符集【重要】

  • ^

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

  • $

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

  • .

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

  • *

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

  • []

    匹配一个指定范围内的字符,如/[ss]ed/匹配sedsed

  • [^]

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

  • \(..\)

    保存匹配的字符,如s/\(linux\)able/\1rslinuxable被替换成linuxrs

  • &

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

  • \<

    锚定单词的开始,如:/\<linux/匹配包含以linux开头的单词的行。

  • \>

    锚定单词的结束,如/linux\>/匹配包含以linux结尾的单词的行。

  • x\{m\}

    重复字符xm次,如:/o\{5\}/匹配包含5o的行。

  • x\{m,\}

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

  • x\{m,n\}

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

6. 实例

删除:d命令

$ sed '2d' example-----删除example文件的第二行。

[root@Jason64-17 sed]# nl sed-1.txt
     1  i am lisp
     2  i have a computer
     3  it is very big
     4  and i have no room
     5  google is very nice
     6  my friends usual alias nice for me
[root@Jason64-17 sed]# sed '3d' sed-1.txt
i am lisp
i have a computer
and i have no room
google is very nice
my friends usual alias nice for me

$ sed '2,$d' example-----删除example文件的第二行到末尾所有行。

[root@Jason64-17 sed]# sed '3,$d' sed-1.txt
i am lisp
i have a computer

$ sed '$d' example-----删除example文件的最后一行。

[root@Jason64-17 sed]# sed '$d' sed-1.txt
i am lisp
i have a computer
it is very big
and i have no room
google is very nice


$ sed '/test/'d example-----删除example文件所有包含test的行。

[root@Jason64-17 sed]# nl sed-2.txt
     1  i am lisp friends
     2  i have a computer that it is my best friends
     3  it is very big
     4  and i have no room
     5  google is very nice
     6  my friends usual alias nice for me
[root@Jason64-17 sed]# sed '/friends/d' sed-2.txt
it is very big
and i have no room
google is very nice

替换:s命令

$ sed 's/test/mytest/g' example-----在整行范围内把test替换为mytest。如果没有g标记,则只有每行第一个匹配的test被替换成mytest

[root@Jason64-17 sed]# nl sed-3.txt
     1  i am lisp friends so lisp's friends is mine
     2  i have a computer that it is my best friends
     3  it is very big
     4  and i have no room
     5  google is very nice
     6  my friends usual alias nice for me
     7  friends is friends
[root@Jason64-17 sed]# sed 's/friends/son/' sed-3.txt
i am lisp son so lisp's friends is mine
i have a computer that it is my best son
it is very big
and i have no room
google is very nice
my son usual alias nice for me
son is friends
[root@Jason64-17 sed]# sed 's/friends/son/g' sed-3.txt
i am lisp son so lisp's son is mine
i have a computer that it is my best son
it is very big
and i have no room
google is very nice
my son usual alias nice for me
son is son

$ sed -n 's/^test/mytest/p' example-----(-n)选项和p标志一起使用表示只打印那些经过sed特殊处理的行。也就是说,如果某一行开头的test被替换成mytest,就打印它。

[root@Jason64-17 sed]# sed -n 's/^i/you/p' sed-3.txt
you am lisp friends so lisp's friends is mine
you have a computer that it is my best friends
yout is very big

$ sed 's/^192.168.0.1/&localhost/' example-----&符号表示替换换字符串中被找到的部份。所有以192.168.0.1开头的行都会被替换成它自已加 localhost,变成192.168.0.1localhost

[root@Jason64-17 sed]# sed 's/^i/fuck &/' sed-3.txt
fuck i am lisp friends so lisp's friends is mine
fuck i have a computer that it is my best friends
fuck it is very big
and i have no room
google is very nice
my friends usual alias nice for me
friends is friends
[root@Jason64-17 sed]# sed 's/^i/linux &-like-You/' sed-3.txt
linux i-like-You am lisp friends so lisp's friends is mine
linux i-like-You have a computer that it is my best friends
linux i-like-Yout is very big
and i have no room
google is very nice
my friends usual alias nice for me
friends is friends

$ sed -n 's/\(linux\)able/\1rs/p' example-----linux被标记为1,所有linuxable会被替换成linuxrs,而且替换的行会被打印出来。

[root@Jason64-17 sed]# sed -n 's/\(fri\)ends/\1day/p' sed-3.txt 
i am lisp friday so lisp's friends is mine
i have a computer that it is my best friday
my friday usual alias nice for me
friday is friends
[root@Jason64-17 sed]# sed -n 's/\(fri\)ends/\1day/gp' sed-3.txt
i am lisp friday so lisp's friday is mine
i have a computer that it is my best friday
my friday usual alias nice for me
friday is friday


$ sed 's#10#100#g' example-----不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,“#”在这里是分隔符,代替了默认的“/”分隔符。表示把所有10替换成100

[root@Jason64-17 sed]# sed 's|friends|soga|g' sed-3.txt
i am lisp soga so lisp's soga is mine
i have a computer that it is my best soga
it is very big
and i have no room
google is very nice
my soga usual alias nice for me
soga is soga

选定行的范围:逗号

$ sed -n '/test/,/check/p' example-----所有在模板testcheck所确定的范围内的行都被打印。

[root@Jason64-17 sed]# nl sed-3.txt
     1  i am lisp friends so lisp's friends is mine
     2  i have a computer that it is my best friends
     3  it is very big
     4  and i have no room
     5  google is very nice
     6  my friends usual alias nice for me
     7  friends is friends
[root@Jason64-17 sed]# sed -n '/am/,/and/p' sed-3.txt
i am lisp friends so lisp's friends is mine
i have a computer that it is my best friends
it is very big
and i have no room

$ sed -n '5,/^test/p' example-----打印从第五行开始到第一个包含以test开始的行之间的所有行。

[root@Jason64-17 sed]# sed -n '3,/friends/p' sed-3.txt
it is very big
and i have no room
google is very nice
my friends usual alias nice for me
[root@Jason64-17 sed]# sed -n '3,/friends$/p' sed-3.txt
it is very big
and i have no room
google is very nice
my friends usual alias nice for me
friends is friends

$ sed '/test/,/check/s/$/sed test/' example-----对于模板testcheck之间的行,每行的末尾用字符串sed test替换。

[root@Jason64-17 sed]# sed '/my/,/and/s/$/-and-test/' sed-3.txt
i am lisp friends so lisp's friends is mine
i have a computer that it is my best friends-and-test
it is very big -and-test
and i have no room-and-test
google is very nice
my friends usual alias nice for me -and-test
friends is friends-and-test
[root@Jason64-17 sed]# sed '/my/,/and/s/^/and-test-/' sed-3.txt
i am lisp friends so lisp's friends is mine
and-test-i have a computer that it is my best friends
and-test-it is very big
and-test-and i have no room
google is very nice
and-test-my friends usual alias nice for me
and-test-friends is friends

多点编辑:e命令

$ sed -e '1,5d' -e 's/test/check/' example-----(-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除15行,第二条命令用check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

[root@Jason64-17 sed]# sed -e '1,2d' -e 's/friends/friday/' sed-3.txt
it is very big
and i have no room
google is very nice
my friday usual alias nice for me
friday is friends

$ sed --expression='s/test/check/' --expression='/linux/d' example-----一个比-e更好的命令是--expression。它能给sed表达式赋值。

[root@Jason64-17 sed]# sed 's/friends/friday/' sed-3.txt     
i am lisp friday so lisp's friends is mine
i have a computer that it is my best friday
it is very big
and i have no room
google is very nice
my friday usual alias nice for me
friday is friends
[root@Jason64-17 sed]# sed --expression = 's/friends/friday/' sed-3.txt
i am lisp friday so lisp's friends is mine
i have a computer that it is my best friday
it is very big
and i have no room
google is very nice
my friday usual alias nice for me
friday is friends

文件读入:r命令

$ sed '/test/r file' example-----file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。

[root@Jason64-17 sed]# cat sed-4.txt
1 2 3 4 5 6 7 8 9 10
[root@Jason64-17 sed]# cat sed-1.txt
i am lisp
i have a computer
it is very big
and i have no room
google is very nice
my friends usual alias nice for me
[root@Jason64-17 sed]# sed '/very/r sed-4.txt' sed-1.txt
i am lisp
i have a computer
it is very big
1 2 3 4 5 6 7 8 9 10
and i have no room
google is very nice
1 2 3 4 5 6 7 8 9 10
my friends usual alias nice for me
[root@Jason64-17 sed]# sed -n '/very/r sed-4.txt' sed-1.txt
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

写入文件:w命令

$ sed -n '/test/w file' example-----example中所有包含test的行都被写入file里。

[root@Jason64-17 sed]# sed -n '/very/w sed-5.txt' sed-1.txt
[root@Jason64-17 sed]# cat sed-5.txt            
it is very big
google is very nice

追加命令:a命令

$ sed /^test/a--->this is a example<-----' example被追加到以test开头的行后

[root@Jason64-17 sed]# sed /^it/a-----so sed-1.txt     
i am lisp
i have a computer
it is very big
-----so
and i have no room
google is very nice
my friends usual alias nice for me
[root@Jason64-17 sed]# sed /very/a'------ ------ ------' sed-1.txt
i am lisp
i have a computer
it is very big
------ ------ ------
and i have no room
google is very nice
------ ------ ------
my friends usual alias nice for me

注意,这里a后面添加的内容如果有空格,必须使用引号(单双都可以)引起来,因为a后面如果碰到空格的话,它就被判断为停止追加了。后面的内容就会被当成文件来看待。

插入:i命令

$ sed '/test/i\\

new line

-------------------------' example

如果test被匹配,则把反斜杠后面的文本插入到匹配行的前面。

[root@Jason64-17 sed]# sed /very/i"------ ->?----- ------" sed-1.txt
i am lisp
i have a computer
------ ->?----- ------
it is very big
and i have no room
------ ->?----- ------
google is very nice
my friends usual alias nice for me
#注意,i参数和a参数后面的内容如果有空格,那么必须加引号,如果没有空格可以加可以不加,但是要是有变量或者特殊字符,那么就要具体情况具体应对了。


下一个:n命令


$ sed '/test/{ n; s/aa/bb/; }' example-----如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变为bb,并打印该行,然后继续。

[root@Jason64-17 sed]# cat sed-1.txt
i am lisp
i have a computer no
it is very big no
and i have no room
google is not no very nice no
my friends not no usual alias no nice for me
[root@Jason64-17 sed]# sed '/very/{n;s/no/YEAH/;}' sed-1.txt
i am lisp
i have a computer no
it is very big no
and i have YEAH room
google is not no very nice no
my friends YEAHt no usual alias no nice for me
[root@Jason64-17 sed]# sed '/very/{n;s/no/YEAH/g;}' sed-1.txt
i am lisp
i have a computer no
it is very big no
and i have YEAH room
google is not no very nice no
my friends YEAHt YEAH usual alias YEAH nice for me

变形:y命令

$ sed '1,10y/abcde/ABCDE/' example-----1--10行内所有abcde转变为大写,注意,正则表达式元字符不能使用这个命令。

[root@Jason64-17 sed]# sed '3,$y/abcde/ABCDE/' sed-2.txt
i am lisp friends
i have a computer that it is my best friends
it is vEry Big
AnD i hAvE no room
googlE is vEry niCE
my friEnDs usuAl AliAs niCE for mE
[root@Jason64-17 sed]# sed '/very/{n;y/abcde/ABCDE/;}' sed-2.txt 
i am lisp friends
i have a computer that it is my best friends
it is very big
AnD i hAvE no room
google is very nice
my friEnDs usuAl AliAs niCE for mE
[root@Jason64-17 sed]# sed '/very/{n;y/o/u/;}' sed-2.txt
i am lisp friends
i have a computer that it is my best friends
it is very big
and i have nu ruum
google is very nice
my friends usual alias nice fur me
[root@Jason64-17 sed]# sed '/very/{n;y/oe/ui/;}' sed-2.txt
i am lisp friends
i have a computer that it is my best friends
it is very big
and i havi nu ruum
google is very nice
my friinds usual alias nici fur mi
[root@Jason64-17 sed]# sed '/very/{n;y/oe/iu/;}' sed-2.txt
i am lisp friends
i have a computer that it is my best friends
it is very big
and i havu ni riim
google is very nice
my friunds usual alias nicu fir mu

注意:这个不使用于正则表达式,而且是单个字符对应单个字符。不是集合。

退出:q命令

$ sed '10q' example-----打印完前10行,退出sed

[root@Jason64-17 sed]# nl sed-3.txt
     1  i am lisp friends so lisp's friends is mine
     2  i have a computer that it is my best friends
     3  it is very big
     4  and i have no room
     5  google is very nice
     6  my friends usual alias nice for me
     7  friends is friends
[root@Jason64-17 sed]# sed 4q sed-3.txt
i am lisp friends so lisp's friends is mine
i have a computer that it is my best friends
it is very big
and i have no room


#以下内容作为了解:#

保持和获取:h命令和G命令

$ sed -e '/test/h' -e '$G example-----在sed处理文件的时候,每一行都被保存在一个叫模式空间的临时缓冲区中,除非行被删除或者输出被取消,否则所有被处理的行都将打印在屏幕上。接着模式空间被清空,并存入新的一行等待处理。在这个例子里,匹配test的行被找到后,将存入模式空间,h命令将其复制并存入一个称为保持缓存区的特殊缓冲区内。第二条语句的意思是,当到达最后一行后,G命令取出保持缓冲区的行,然后把它放回模式空间中,且追加到现在已经存在于模式空间中的行的末尾。在这个例子中就是追加到最后一行。简单来说,任何包含test的行都被复制并追加到该文件的末尾。

保持和互换:h命令和x命令

$ sed -e '/test/h' -e '/check/x' example -----互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换。

7. 脚本

sed脚本是一个sed的命令清单,启动sed时以-f选项引导脚本文件名。sed对于脚本中输入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多个命令,要用分号分隔。以#开头的行为注释行,且不能跨行。

8. 小技巧

在sed的命令行中引用shell变量时要使用双引号,而不是通常所用的单引号。



===============

update 2018年08月07日17:46:10

sed 在指定行插入文本

sed -i '12 isource /etc/profile' /etc/init.d/lisp

表示,在/etc/init.d/lisp文本中的第12行进行插入source /etc/profile