sed基本用法简介

来源:互联网 发布:数据库脚本怎么写 编辑:程序博客网 时间:2024/06/06 02:13

       sed是stream editor, 也就是流编辑器, 实际上就是linux中的一个命令, 作用很强大。 搞linux开发的人, 不能不熟悉该命令, 否则, 何以立足于江湖呢? 在本文中, 我们来一起玩玩sed, 简要介绍其用法。 

       因为sed很强大, 所以难以一次介绍所有的内容。 以后遇到新的东东, 再往本博文中添加。 现在, 能学一点就是一点。 还是老规矩, 在理解的基础上, 大量练习和实践。以实践操作为荣, 以只看不练为耻。


       在本文中,我们的文件为taoge.txt, 其中的内容为(注意, 在本文中, 我们不改变这个文件中的内容):

My favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you like
        现在要输出taoge.txt的第2-3行, 该怎么做呢? 我们可能会这么搞:
Administrator@51B6904C3C8A485 ~/learn_sed$ cat taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$ head -n 3 taoge.txt | tail -n 2I like watermelon so muchMy favorite subject is mathAdministrator@51B6904C3C8A485 ~/learn_sed$
        用head和tail的联合, 是个好思路, 但还是不如sed痛快, 让我们来看看sed, 如下:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "2,3"p taoge.txtI like watermelon so muchMy favorite subject is mathAdministrator@51B6904C3C8A485 ~/learn_sed$
       说了那么多, 总算是见到了sed了。



       sed的基本格式是:sed 【选项】【"命令"】 taoge.txt  .我们来大致介绍一下sed的基本用法。sed的选项通常有-n, -i, -e等, 比较容易掌握, 本文我们重点介绍"命令"部分。



       一. p命令, 意思是print, 用于打印。

       1.  打印文件,类似于cat命令:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed p taoge.txtMy favorite fruit is watermelonMy favorite fruit is watermelonI like watermelon so muchI like watermelon so muchMy favorite subject is mathMy favorite subject is mathI like math so muchI like math so muchMy favorite pet is dogMy favorite pet is dogI like dog so muchI like dog so muchMy English name is EricMy English name is EricBut you can also call me taoge if you likeBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
      我们看到, 重复了, 为什么呢?  因为sed在处理信息时, 会把待处理的信息也输出, 那怎样抑制输出待处理的信息呢? 且看-n选项:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n p taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
      当然, 你也可以用:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "1,$"p taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       其中, 1表示第一行, $表示最后一行, 也就是输出第一行到最后一行的内容, 而这就是整个文件。


       2. 打印行(第3行到最后一行)

Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "3,$"p taoge.txtMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       这个就不需要我多说了吧, 比所谓的head, tail高级多了。


       3. 过滤行, 类似于grep命令, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "/like/"p taoge.txtI like watermelon so muchI like math so muchI like dog so muchBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


      4. 从第1行打印, 直到匹配dog的行,这个貌似用得不多
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "1,/dog/p" taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogAdministrator@51B6904C3C8A485 ~/learn_sed$


       5. 打印开始匹配dog的行, 到匹配taoge的行:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -n "/dog/,/taoge/"p taoge.txtMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$



       二. s命令,用于替换。

       1.  简单替换

Administrator@51B6904C3C8A485 ~/learn_sed$ echo "i love you, you love me" | sed "s/love/like/"i like you, you love meAdministrator@51B6904C3C8A485 ~/learn_sed$ echo "i love you, you love me" | sed "s/love/like/g"i like you, you like meAdministrator@51B6904C3C8A485 ~/learn_sed$
        可见, g表示一行上的所有love都替换成like, 否则, 只替换第一个。 继续看:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/taoge/haha/g" taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me haha if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       其实, 上面这个替换,并不会改变taoge.txt本身的内容, 那要改变到文件中, 怎么办呢? 

       (1) 将结果重定向, 如sed "s/taoge/haha/g" taoge.txt > taoge.txt.bak

       (2) 使用-i选项, 如sed -i "s/taoge/haha/g" taoge.txt


       2.  仅仅针对某一行进行替换, 如:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "2s/so/very/g" taoge.txtMy favorite fruit is watermelonI like watermelon very muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


      3. 针对多行进行替换, 如:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "1,4s/water//g" taoge.txtMy favorite fruit is melonI like melon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

      4. 只替换第1个i, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/i/I/1" taoge.txtMy favorIte fruit is watermelonI lIke watermelon so muchMy favorIte subject is mathI lIke math so muchMy favorIte pet is dogI lIke dog so muchMy EnglIsh name is EricBut you can also call me taoge If you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       只替换第2个i, 如下:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/i/I/2" taoge.txtMy favorite fruIt is watermelonI like watermelon so muchMy favorite subject Is mathI like math so muchMy favorite pet Is dogI like dog so muchMy English name Is EricBut you can also call me taoge if you lIkeAdministrator@51B6904C3C8A485 ~/learn_sed$
       替换第二个到最后一个o, 如下:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/o/O/2g" taoge.txtMy favorite fruit is watermelOnI like watermelon sO muchMy favorite subject is mathI like math so muchMy favorite pet is dOgI like dog sO muchMy English name is EricBut you can alsO call me taOge if yOu likeAdministrator@51B6904C3C8A485 ~/learn_sed$

      5. 多个模式匹配
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "1,4s/I/i/g; 5,6s/I/you/g" taoge.txtMy favorite fruit is watermeloni like watermelon so muchMy favorite subject is mathi like math so muchMy favorite pet is dogyou like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       当然, 也可以用扩展模式(是该介绍-e选项了):
Administrator@51B6904C3C8A485 ~/learn_sed$ sed -e "1,4s/I/i/g" -e "5,6s/I/you/g" taoge.txtMy favorite fruit is watermeloni like watermelon so muchMy favorite subject is mathi like math so muchMy favorite pet is dogyou like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


       6. 利用&表示匹配到的东东, 如下:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/so/{&}/g" taoge.txtMy favorite fruit is watermelonI like watermelon {so} muchMy favorite subject is mathI like math {so} muchMy favorite pet is dogI like dog {so} muchMy English name is EricBut you can al{so} call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


      7. 在每行前面加一些东西,  来个有意思点的, 颇有点C/C++注释的感觉:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/^/\/\/ /" taoge.txt// My favorite fruit is watermelon// I like watermelon so much// My favorite subject is math// I like math so much// My favorite pet is dog// I like dog so much//// My English name is Eric// But you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
       实际上, 这个就实现了对代码进行注释了。 可能有的朋友不太清楚, 那我来解释一下:

        a. ^表示一行的开口(当然, 如果你熟悉正则表达式, 那就更好了)

        b. \/的\表示转义, /表示斜杠, 所以\/\/就是//了.

       整体功能就是, 对每行的头加了//

  

       8. 在每行的末尾加点东西, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "s/$/  \/\/ comment /" taoge.txtMy favorite fruit is watermelon  // commentI like watermelon so much  // commentMy favorite subject is math  // commentI like math so much  // commentMy favorite pet is dog  // commentI like dog so much  // comment  // commentMy English name is Eric  // commentBut you can also call me taoge if you like  // commentAdministrator@51B6904C3C8A485 ~/learn_sed$
       其实, 说白了, 还是正则表达式。


       三. c命令, 用于行替换。

       1. 替换第2行, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "2 c hehe" taoge.txtMy favorite fruit is watermelonheheMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

    

      2. 仅仅替换有dog的行, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "/dog/c cat" taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchcatcatMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


       四. i命令, 表示行插入。

       1.  在第一行前面插入, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "1 i oh, my god" taoge.txtoh, my godMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

       

       2. 在最后一行前面插入, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "$ i oh, my god" taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is Ericoh, my godBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

        五. a命令, 用于append,也就是依附

        1. 在第一行后依附, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "1 a oh, my god" taoge.txtMy favorite fruit is watermelonoh, my godI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

      

       2. 在最后一行后依附, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "$ a oh, my god" taoge.txtMy favorite fruit is watermelonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeoh, my godAdministrator@51B6904C3C8A485 ~/learn_sed$

       

      六. d命令, 表示删除所匹配的行。

       1. 删除有is的行, 如下:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "/is/d" taoge.txtI like watermelon so muchI like math so muchI like dog so muchBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

       

       2.  删除第1行:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "1d" taoge.txtI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
      删除第2行:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "2d" taoge.txtMy favorite fruit is watermelonMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
     删除第2-4行:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "2,4d" taoge.txtMy favorite fruit is watermelonMy favorite pet is dogI like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$
      删除第3到最后一行, 如下:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "3,$"d taoge.txtMy favorite fruit is watermelonI like watermelon so muchAdministrator@51B6904C3C8A485 ~/learn_sed$
      或者:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed '3,$d' taoge.txtMy favorite fruit is watermelonI like watermelon so muchAdministrator@51B6904C3C8A485 ~/learn_sed$
      但如下有错误:
Administrator@51B6904C3C8A485 ~/learn_sed$ sed "3,$d" taoge.txtsed: -e expression #1, char 2: unexpected `,'Administrator@51B6904C3C8A485 ~/learn_sed$


      七. N命令, 将下一行内容纳入缓冲区做匹配。

      1. 先看:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "N;s/me/XX/" taoge.txtMy favorite fruit is waterXXlonI like watermelon so muchMy favorite subject is mathI like math so muchMy favorite pet is dogI like dog so muchMy English naXX is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$

      

      2. 再看:

Administrator@51B6904C3C8A485 ~/learn_sed$ sed "N;1,6s/\n/, /" taoge.txtMy favorite fruit is watermelon, I like watermelon so muchMy favorite subject is math, I like math so muchMy favorite pet is dog, I like dog so muchMy English name is EricBut you can also call me taoge if you likeAdministrator@51B6904C3C8A485 ~/learn_sed$


       OK, 本文先介绍到这里了, 后续遇到sed的其他用法, 会继续记录在此。






0 0
原创粉丝点击