• 【转】sed 的参数


    一、15个参数
    1、r 从文件读入
    [root@watchout2 ~]# cat file
    1
    2
    3
    4
    5
    [root@watchout2 ~]# cat newfile 
    a
    b
    c
    d
    e
    [root@watchout2 ~]# sed '/a/r file' newfile (读入文件file,并显示在newfile文件中匹配行a之后)
    a
    1
    2
    3
    4
    5
    b
    c
    d
    e
    [root@watchout2 ~]# touch /file
    [root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file 
    [root@watchout2 ~]# sed '/a/r /file' newfile  (读入的文件在不同的路径)
    a
    aaaaaaaaaaaaaaaaaaaaaaaaaa
    b
    c
    d
    e
    [root@watchout2 ~]#
     
    2、w写入文件
    [root@watchout2 ~]# sed '/a/w bo' newfile 
    a
    b
    c
    d
    e
    [root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )
    a
     
    3、a 追加命令
    [root@watchout2 ~]# sed '/b/a obo' newfile 
    a
    b
    bobo
    c
    d
    e
     
    4、i 插入
    [root@watchout2 ~]# sed '/a/i obo' newfile 
    bobo
    a
    b
    c
    d
    e
     
    5、n 下一个
    [root@watchout2 ~]# sed -n '/a/{n;p}' newfile(打印匹配行的下一行) 
    b
     
    [root@watchout2 ~]# sed '/a/{n;s/b/c/p}' newfile 
    a
    c
    c
    c
    d
    e
     
    6、y 变形命令
    [root@watchout2 ~]# sed '1,3y/abc/ABC/' newfile 
    A
    B
    C
    d
    e
    y命令就是将小写转换成了大写,正则表达式元字符不能使用这个命令。
     
    7、q 退出命令
    [root@watchout2 ~]# sed '3q' newfile 
    a
    b
    c
    打印前三行后退出。
     
    8、h命令   是将pattern space 模式空间(临时缓冲区)的内容复制到holding buffer保持缓冲区
    9、G命令 是将holding buffer中的内容取得,尔后放回pattern space中,且追加到相应行的末 尾 
        g命令是将holding buffer 中的内容取得,尔后放回pattern space 中,且替换相应的行
     
    [root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile 
    a
    b
    c
    d
    a
    e
    h命令会把a匹配行,放入保持缓冲区,G命令会把保持缓冲区中的内容放入模式空间,并追加到匹配行d的下一行。
     
    [root@watchout2 ~]# sed -e '/a/h' -e '$G' newfile 
    a
    b
    c
    d
    e
    a
    与上相同,只是$代表最后一行。
     
    [root@watchout2 ~]# sed -e '/a/h' -e '/b/g' newfile 
    a
    a
    c
    d
    e
    [root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile 
    a
    b
    c
    d
    a
     
    以上h命令会把匹配行a,放入保持缓冲区,g命令会读取保持缓冲区的内容,将匹配行b(第二个例子就是$最后一行)替换为a 。注:a将覆盖匹配行b(第二个例子就是$最后一行)
     
    10、x命令 是pattern space模式空间将被holding buffer保持缓冲区中的内容替换
    [root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile 
    a
    b
    c
    a
    e
    匹配行d 将被匹配行a替换。
     
    11、-n选项取消sed的默认行为,sed 默认行为是-p ,
    root:/tmp>sed '/6/p' num   -p参数打印num的内容,尔后匹配“6”在次打印6,所以6会出现两次。
    1
    2
    3
    4
    5
    6
    6
    7
    8
    root:/tmp>sed -n '/6/p' num  -n选项取消sed的默认行为(-p ),所以只打印“6”
    6
    root:/tmp>
     
    12、删除:d命令
    删除第6行
    root:/tmp>sed '6d' num
    1
    2
    3
    4
    5
    7
    8
    root:/tmp>
     
    从第6行删除到行尾
    root:/tmp>sed '6,$d' num
    1
    2
    3
    4
    5
    root:/tmp>
     
    d删除最后一行
    root:/tmp>sed '$d' num
    1
    2
    3
    4
    5
    6
    7
    root:/tmp>
     
    d删除匹配行
    root:/tmp>sed '/6/d' num
    1
    2
    3
    4
    5
    7
    8
    root:/tmp>
     
    13、替换:s命令
    s表示替换,g表示作用范围整个行,如果没有g标志则只有每行第一个被替换
    root:/tmp>sed 's/1/8/g' num    (sed默认有-p参数打印搜索后的所有行。如g后面加p,那么匹配行就会打印两次)
    8
    2
    3
    4
    5
    6
    7
    8
    root:/tmp>
     
    行首“1”开头被替换为“8”
    root:/tmp>sed 's/^1/8/g' num 
    8
    2
    3
    4
    5
    6
    7
    8
    root:/tmp>
     
    &符号表示替换字符串中被找到的部分,所以每个数字后面被追加.6789
    root:/tmp>sed 's/[0-9]/&.6789/g' num
    1.6789
    2.6789
    3.6789
    4.6789
    5.6789
    6.6789
    7.6789
    8.6789
    root:/tmp>
     
    (..) 保存匹配的字符到标签1中。  s/(5)/1.6789/  蓝色部分保存到标签1中。从表达式最左边开始,向右最多可以使用9个标签
    root:/tmp>sed 's/(5)/1.6789/g' num
    1
    2
    3
    4
    5.6789
    6
    7
    8
    root:/tmp>sed 's/([0-9])/1.6789/g' num
    1.6789
    2.6789
    3.6789
    4.6789
    5.6789
    6.6789
    7.6789
    8.6789
    root:/tmp>
     
    s后面的字符是分隔搜索字符串和替换字符串的分隔符。默认分隔符是斜杠,不论什么字符紧跟s命令都被认为是新的分隔符
    root:/tmp>sed 's#6#shell#g' num
    1
    2
    3
    4
    5
    shell
    7
    8
    root:/tmp>
     
    14、多点编辑:e命令
    root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num
    7
    8.shell
    root:/tmp>
     
    15、-f  引导sed脚本文件名
     
     
    16、选定行的范围:逗号
     
    从第一行到第五行
    root:/tmp>sed -n '1,5p' num
    1
    2
    3
    4
    5
     
    从第一行到第五行,然后在搜索
    root:/tmp>sed '1,5s/3/3.linux/g' num
    1
    2
    3.linux
    4
    5
    6
    7
    8
     
    显示从第三行到行首为6的中间所有行
    root:/tmp>sed -n '3,/^6/p' num
    3
    4
    5
    6
    root:/tmp>
     
     
    匹配1和5后面追加“******Total********”
    root:/tmp>sed '/1/,/5/s/$/******Total********/g' num
    1******Total********
    2******Total********
    3******Total********
    4******Total********
    5******Total********
    6
    7
    8
    root:/tmp>
     
    转自:http://blog.chinaunix.net/uid-8656705-id-2017935.html
  • 相关阅读:
    HTML标签
    Web标准
    Web服务器 (获取域名)
    网站favicon.ico 图标
    模块化
    外边距
    h5css产品模块设计
    用策略模式(自定义注解+包扫描)解决if else 太多的问题
    方法区 永久代 元空间 常量池
    javac jar java
  • 原文地址:https://www.cnblogs.com/nhlinkin/p/3636621.html
Copyright © 2020-2023  润新知