美文网首页
35 sed 命令

35 sed 命令

作者: StarShift | 来源:发表于2016-10-27 02:04 被阅读40次

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

    使用这段文本做演示:

    $ cat pets.txt
    This is my cat
      my cat's name is betty
    This is my dog
      my dog's name is frank
    This is my fish
      my fish's name is george
    This is my goat
      my goat's name is adam
    

    替换

    把其中的my字符串替换成your,下面的语句应该很好理解(s表示替换命令,/my/表示匹配my,/your/表示把匹配替换成Hao Chen’s,/g 表示一行上的替换所有的匹配):

    root@ubuntu:~# sed "s/my/your/g" pets.txt 
    This is your cat
      your cat's name is betty
    This is your dog
      your dog's name is frank
    This is your fish
      your fish's name is george
    This is your goat
      your goat's name is adam
    
    

    再注意:上面的sed并没有对文件的内容改变,只是把处理过后的内容输出,如果你要写回文件,你可以使用重定向。
    或使用 -i 参数直接修改文件内容:

    root@ubuntu:~# 
    root@ubuntu:~# cat pets.txt 
    This is my cat
      my cat's name is betty
    This is my dog
      my dog's name is frank
    This is my fish
      my fish's name is george
    This is my goat
      my goat's name is adam
    root@ubuntu:~# sed -i "s/my/your/g" pets.txt 
    root@ubuntu:~# cat pets.txt 
    This is your cat
      your cat's name is betty
    This is your dog
      your dog's name is frank
    This is your fish
      your fish's name is george
    This is your goat
      your goat's name is adam
    
    

    在每一行前面加一点东西:

    sed "s/^/#/g" pets.txt
    sed "s/^This/#This/g" pets.txt
    sed "s/^This/#/g" pets.txt
    
    root@ubuntu:~# sed "s/^/#/g" pets.txt
    #This is your cat
    #  your cat's name is betty
    #This is your dog
    #  your dog's name is frank
    #This is your fish
    #  your fish's name is george
    #This is your goat
    #  your goat's name is adam
    root@ubuntu:~# sed "s/^This/#This/g" pets.txt
    #This is your cat
      your cat's name is betty
    #This is your dog
      your dog's name is frank
    #This is your fish
      your fish's name is george
    #This is your goat
      your goat's name is adam
    root@ubuntu:~# sed "s/^This/#/g" pets.txt
    # is your cat
      your cat's name is betty
    # is your dog
      your dog's name is frank
    # is your fish
      your fish's name is george
    # is your goat
      your goat's name is adam
    

    在每一行后面加一点东西

    root@ubuntu:~# sed "s/$/\!\!\!/g" pets.txt
    This is your cat!!!
      your cat's name is betty!!!
    This is your dog!!!
      your dog's name is frank!!!
    This is your fish!!!
      your fish's name is george!!!
    This is your goat!!!
      your goat's name is adam!!!
    

    在sed中可用的正则表达式的锚定字符:

    • <abc 表示以abc开头的词
    • abc> 表示以abc结尾的词

    比如我们想把your换成her:

    root@ubuntu:~# sed "s/\<you/he/g" pets.txt
    This is her cat
      her cat's name is betty
    This is her dog
      her dog's name is frank
    This is her fish
      her fish's name is george
    This is her goat
      her goat's name is adam
    

    又想把her换成his:

    root@ubuntu:~# sed "s/\<you/he/g" pets.txt > herpets.txt
    root@ubuntu:~# sed "s/er\>/is/g" herpets.txt 
    This is his cat
      his cat's name is betty
    This is his dog
      his dog's name is frank
    This is his fish
      his fish's name is george
    This is his goat
      his goat's name is adam
    

    另外一个例子

    root@ubuntu:~# cat html.txt 
    <b>This</b> is what <span style="text-decoration: underline;">I</span> meant. Understand?
    

    假设我们想去掉html标签,使用如下正则表达式:

    root@ubuntu:~# sed "s/<.*>//g" html.txt 
     meant. Understand?
    

    得到的结果是错误的,为什么呢?
    因为正则表达式<.*> 表示<>中间的任意并且任意数量字符。其匹配结果为:

    <b>This</b> is what <span style="text-decoration: underline;">I</span>
    

    使用如下表达式可以正常匹配:

    root@ubuntu:~#  sed 's/<[^>]*>//g' html.txt
    This is what I meant. Understand?
    

    其中方括号内的内容,指了除了>的字符重复0次或多次。

    在sed中使用量词

    只替第三行的pattern

    root@ubuntu:~# sed '3s/your/my/' pets.txt 
    This is your cat
      your cat's name is betty
    This is my dog
      your dog's name is frank
    This is your fish
      your fish's name is george
    This is your goat
      your goat's name is adam
    
    

    替换3,6行个pattern

    root@ubuntu:~# sed '3,6s/your/my/' pets.txt 
    This is your cat
      your cat's name is betty
    This is my dog
      my dog's name is frank
    This is my fish
      my fish's name is george
    This is your goat
      your goat's name is adam
    

    替换每一行的第一个s

    root@ubuntu:~# sed 's/s/S/1' pets.txt 
    ThiS is your cat
      your cat'S name is betty
    ThiS is your dog
      your dog'S name is frank
    ThiS is your fish
      your fiSh's name is george
    ThiS is your goat
      your goat'S name is adam
    

    替换每一行第2个以后的s:

    root@ubuntu:~# sed 's/s/S/2g' pets.txt 
    This iS your cat
      your cat's name iS betty
    This iS your dog
      your dog's name iS frank
    This iS your fiSh
      your fish'S name iS george
    This iS your goat
      your goat's name iS adam
    

    多个匹配

    如果一次需要匹配多个模式,可以使用下面的方法:

    root@ubuntu:~# sed '1,3s/your/my/g; 3,$s/This/That/g' pets.txt 
    This is my cat
      my cat's name is betty
    That is my dog
      your dog's name is frank
    That is your fish
      your fish's name is george
    That is your goat
      your goat's name is adam
    
    

    可以使用&来当做被匹配的变量:

    root@ubuntu:~# sed 's/your/[&]/g' pets.txt 
    This is [your] cat
      [your] cat's name is betty
    This is [your] dog
      [your] dog's name is frank
    This is [your] fish
      [your] fish's name is george
    This is [your] goat
      [your] goat's name is adam
    

    分组

    root@ubuntu:~# cat my.txt 
    This is your cat, your cat's name is betty
    This is your dog, your dog's name is frank
    That is your fish, your fish's name is george
    That is my goat, my goat's name is adam
    root@ubuntu:~# sed 's/This is your \([^,]*\), .* is \(.*\)/\1:\2/g' my.txt
    cat:betty
    dog:frank
    That is your fish, your fish's name is george
    That is my goat, my goat's name is adam
    
    

    相关文章

      网友评论

          本文标题:35 sed 命令

          本文链接:https://www.haomeiwen.com/subject/pejquttx.html