sed命令常见使用杂记

重要声明:本文是作者在学习过程中做的笔记,格式上可能不太令人满意,希望有意学习的同学谅解。(更为详细的实例演示前往作者另外两篇博客:http://blog.csdn.net/qq_17586821/article/details/52052761http://blog.csdn.net/qq_17586821/article/details/52093416

sed:stream editor 

一次只读一行,只处理一行,不对源文件做处理,只输出到屏幕上,行编辑器,流编辑器,非交互式

格式:
       sed [options] "script" file ...

选项:
      -n:静默模式,不输出模式空间内的内容;
      默认情况下会打印空间模式的内容-->原来的内容和新的内容都会显示
       -r:扩展的正则表达式
       -f 文件:指定sed脚本文件
       -e 'script' -e 'script':指定多个编辑指令
       -i: 直接编辑原文件

编辑命令:
        d:删除
            sed '1,3d' /etc/fstab #删除前三行
            sed '/^#/d' /etc/fstab  #删除以“#”开头的行
            sed '/^\//d' /etc/fstab #删除以“/”开头的行
            sed '3,/^\//d' /etc/fstab #删除第三行到第一个以“/”开头的行
            sed '/^#/,/^\//d' /etc/fstab

p:打印,经常和-n选项一起使用
            sed '1,3p' /etc/fstab
            sed -n '1,3p' /etc/fstab
            sed '/^\//p' /etc/fstab
            sed -n '/^\//p' /etc/fstab

i\text:在被指定到的行前面插入文本
            sed '/^\//i\this line starts with a /' /etc/fstab

a\text:在被指定的行的下面插入文本
            sed '/^\//a\this line starts with a /' /etc/fstab
            sed '/^[[:upper:]]/a\First Line.\nSedcond Line.' /etc/fstab     (\n:换行)

r 文件:在指定位置把另外一个文件的内容插入
            sed '/^UUID/r /etc/issue' /etc/fstab
            sed '/^#/r /etc/issue' /etc/fstab

w 文件:将符合条件的所有行保存至指定文件中(覆盖式追加)
            sed '1,4w /etc/test.txt' /etc/fstab
            sed -n '/^\//w /etc/test.txt' /etc/fstab

=:显示符号条件的行的行号
            sed '/^\//=' /etc/fstab

s///:查找条件可以使用模式,但是要替换的内容不行;字符串分隔符/可以使用其他的,如@


地址定界:自定义的起始行到结束行

num1,num2   
    /pattern1/,/pattern/
    /pattern/   

用法:sed [options] 'addr1[,addr2]编辑命令' file ...

正则表达式

   基本的正则表达式

   字符:
          .  []  [^]
    次数:
          *  \?  \{m,n\}
锚定:
^  $  \<或者\b(词首)  \>或者\b(词尾) 
分组:
               \(\)
            引用:
               \1,\2,……


----------


扩展的正则表达式 

   字符:
      .  []  [^]    [[:upper:]] [[:lower:]] [[:alnum:]] [[:punct:]]
   次数:
      * ? {m,n} +
   锚定:
               ^ $ \<或者\b(词首)  \>或者\b(词尾)
            或:
               a|b
            分组:
               \(\)
            引用:
               \1,\2,……

sed '地址定界s@查找条件@替换文件@'

修饰符
g:global--全局替换
i:ignore-case--不区分字符大小写

----------


下面的演示将会基于这个文件(文件名是inputfile,共13行):
> This is a Certificate Request file:
> 
> It should be mailed to zawu@seu.edu.cn
> 
>============================================
> Certificate Subject:
>
>   (这里有4个空格)/O=Gird/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus
> 
> The above strng is known as your user certificate subject, and it uniquely identifies this user.$88
> To install this user certificate,please save this e-main message into the following file.
>
> /home/globus/.globus/usercert.pem
----------


    sed -n '1p' inputfile
    sed '1p' inputfile
    sed -n '3,6p' inputfile
    sed -n '/certificate/p' inputfile
    sed -n '/Certificate/=' inputfile 打印被模式匹配到的行的行号
    sed -n -e '/Certificate/p' -e '/Certificate/=' inputfile
    sed '/file:/a\We append a new line.' inputfile 追加命令\a 

        append.sed:
            #!/bin/sed -f 
            /file:/a\
            We append a new line.\
            We append another new line.
        chmod u+x append.sed 
        ./append.sed inputfile

    sed -n '/\./p' inputfile sed匹配元字符需要转义
    sed -n '/\$/p' inputfile

    sed -n '$p' inputfile 这两条命令是等价的
    sed -n '$'p inputfile

    sed -n '/.*bus/p' inputfile
    sed -n '2,10!p' inputfile
    sed -n '1!p' inputfile
    sed -n '/seugrid/,$p' inputfile
    sed -n '3,/seugrid/p' inputfile

        insert.sed:
            #!/bin/sed -f
            /file:/i\
            We insert a new line.
        chmod u+x insert.sed 
        ./insert.sed inputfile

        modify.sed  
            #!/bin/sed -file
            /file:/c\
            We modify this line.
        chmod u+x modify.sed 
        ./modify.sed inputfile

    sed '1d' inputfile
    sed '1,10d' inputfile
    sed '5,$d' inputfile

        delete.sed:
            #!/bin/sed -f
            /[Cc][Ee][Rr][Tt][Ii][Ff][Ii][Cc][Aa][Tt][Ee]/d 
        chmod u+x delete.sed
        ./delete.sed inputfile

    sed 's/Certificate/CERTIFICATE/' inputfile 打印全部文件内容
    sed -n 's/Certificate/CERTIFICATE/p' inputfile 只打印匹配行
    sed -n 's/Certificate/CERTIFICATE/' inputfile 不打印任何东西
    sed -n 's/seu/njue/p' inputfile 只替换匹配行的第一个匹配字符串
    sed -n 's/seu/njue/pg' inputfile 替换匹配行的所有匹配字符串
    sed -n 's/seu/njue/2p' inputfile 替换匹配行的第二次匹配到的字符串
    sed -n 's/seu/njue/w output' inputfile 将结果写入文件output
    sed -n '1,5 w output' inputfile 将结果写入文件output
    sed -n '/globus/w output' inputfile 将结果写入文件output
    sed '/Certificate/r otherfile' inputfile 从otherfile读取内容,追加到匹配字符串(Certificate)所在行的后边

    &符号保存了被替换的字符串
    sed -n 's/seu/(&)/pg' inputfile 等价于 sed -n 's/seu/(seu)/pg' inputfile
    sed '5 q' inputfile 打印前5行,立即退出
    sed -n '/.r.*/p' inputfile
    sed '/.r.*/q' inputfile 匹配到第一个满足要求的字符串就退出
    sed 'y/fmj/FMJ/' inputfile 一个一个对应替换
    sed 'y/fmj/FMJX/' inputfile 替换失败

    sed -n -e '/Certificate/p' -e '/Certificate/=' inputfile
    等价于
    sed -n '/Certificate/{p;=}' inputfile

    sed '/Certificate/{s/i/I/g;s/le/99/;}' inputfile
    sed '/Certificate/{n;s/ll/99/;}' inputfile

    sed 's/globus/GLOBUS/; s/seugrid/SEUGRID/' inputfile ;分割多个编辑命令(-e和{}也可以)
    sed -e 's/globus/GLOBUS/' -e 's/seugrid/SEUGRID/' inputfile

    sed '
    >s/globus/GLOBUS/
    >s/seugrid/SEUGRID/
    >$d' inputfile 同时使用多个编辑命令

    sed -e '/Subject/h' -e '/seugrid/x' -e '$G' inputfile

    该命令是由3个—e选项带上三个编辑命令组成的  第一个—e选项后的编辑命令将Subject关键字的匹配行写入保持缓冲区--h命令将模式缓冲区的内容复制到保持缓冲区;
    第二个-e选项后的编辑命令是遇见seugrid关键字的匹配行时,将保持缓冲区的内容输出,并将seugrid关键字的匹配行写入保持缓冲区。因此,原来seugrid行的位置变为了Subject行--x命令互换模式缓冲区和保持缓冲区的内容;

    第三个-e选项后的编辑命令表示到最后一行时,将保持缓冲区的内容追加到模式缓冲区--G命令的意义,因此,最后一行是seugrid行
    h和H、g和G是两组对应的命令,h和H命令是模式缓冲区的内容那个替换保持缓冲区的内容,不过h是副本,即将保持缓冲区的旧内容覆盖掉,而H是追加,即在保持缓冲区的内容上增加新的内容;g和G命令是保持缓冲区的内容替换模式缓冲区的内容,同样,g是副本,G是追加。

    sed -e '/Subject/h' -e '/seugrid/H' -e '$G' input
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值