• sed应用


    sed应用

    一.sedsed命令的基本语法

    sed OPTIONS… [SCRIPT] [INPUTFILE…]
    
    常用的选项:
    
    -n,–quiet: 不输出模式空间中的内容
    
    -i: 直接编辑原文件,默认不对原文件进行操作
    
    -e: 可以使用多个命令(脚本)进行操作
    
    -f /path/from/sed_script: 从指定的文本中读取处理脚本
    
    -r: 使用扩展正则表达式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1. 删除

    • 删除文件中的内容
    语法
    [address]command
    [line-address]command
    
    
    1.删除以pwpolicy开头的内容
    [root@node1 ~]# cat -n anac.cfg 
         1  %anaconda
         2  pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
         3  pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
         4  pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
         5  %end
    [root@node1 ~]# sed '/^pwpolicy/d' anac.cfg 
    %anaconda
    %end
    
    2. 删除第四行
    [root@node1 ~]# sed '4d' anac.cfg 
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    %end
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2. 替换 1.0

    2.1 语法
    [address]s/pattern/seplacement/flages
    地址 模式 替换 
    flages:
    n 1-512之间的一个数字,表示对文本模式中指定模式第n次出现
    g 全局替换,默认只替换第一个
    p 如果成功替换则打印
    w 将模式中的内容写入文件中
    i 不区分大小写
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.2 例1
    转义符默认为/ 也可以用# !
    
    将/usr/local/src 改成/usrs/local/bin
    1. 
    [root@node1 ~]# cat abc 
    /usr/local/src
    [root@node1 ~]# sed 's/\/usr\/local\/src/\/usrs\/local\/bin/' abc 
    /usrs/local/bin
    [root@node1 ~]# 
    
    2.
    [root@node1 ~]# sed 's#/usr/local/src#/usrs/local/bin#' abc /usrs/local/bin
    
    3.
    [root@node1 ~]# sed 's!/usr/local/src!/usrs/local/bin!' abc 
    /usrs/local/bin
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    2.3 例2
    Tab和\t制表符
    
    将文本的制表符第二个或者第三个换成>
    1.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    [root@node1 ~]# sed 's/\t/>/2' abc 
    Column1 Column2>Column3 Column4
    [root@node1 ~]# sed 's/\t/>/3' abc 
    Column1 Column2 Column3>Column4
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    2.4 例3
    替换元字符
    & 用正则表达式匹配的内容进行替换
    \n 换行
    \ 转义符
    
    1.将文件换行
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    [root@node1 ~]# sed 's/\t/\
    > /2' abc
    Column1 Column2
    Column3 Column4
    [root@node1 ~]# sed 's/\t/\n/2' abc
    Column1 Column2
    Column3 Column4
    [root@node1 ~]# sed 's/\t/\n/3' abc
    Column1 Column2 Column3
    Column4
    [root@node1 ~]# 
    
    2. .Ah "Major Heading"替换@A HEAD = Major Heading
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    [root@node1 ~]# sed '/^\.Ah/{ 
    > s/\.Ah */\
    > @A HEAD = /
    > s/"//g
    > s/$/\
    > /}' abc
    Column1 Column2 Column3 Column4
    
    @A HEAD = Major Heading
    
    [root@node1 ~]# 
    
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    1)
    [root@node1 ~]# sed '/^\.Ah/p' abc
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    .Ah "Major Heading"
    
    2)
    [root@node1 ~]# sed '/^\.Ah/s/\.Ah */@A HEAD = /' abc
    Column1 Column2 Column3 Column4
    @A HEAD = "Major Heading"
    [root@node1 ~]# 
    
    3)
    [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = /;s/"//g}' abc Column1 Column2 Column3 Column4
    @A HEAD = Major Heading
    [root@node1 ~]# 
    
    4)
    [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g}' abc  
    Column1 Column2 Column3 Column4
    
    @A HEAD = Major Heading
    
    5)
    [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g};s/$/\n/' abc 
    Column1 Column2 Column3 Column4
    
    
    @A HEAD = Major Heading
    
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    2.5 例4
    & 匹配整行内容
    1.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    [root@node1 ~]# sed "s/ORA/& Associates, Inc./g" abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc. Associates, Inc.
    [root@node1 ~]# 
    
    
    ORA Associates, Inc.替换O' Reilly ORA Associates, Inc.
    2.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    1)
    [root@node1 ~]# sed -r "s/ORA (.*)/O' Reilly &/g" abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    O' Reilly ORA Associates, Inc.
    2)
    [root@node1 ~]# sed -r "s/ORA/O' Reilly &/g" abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    O' Reilly ORA Associates, Inc.
    [root@node1 ~]# 
    
    
    3.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    
    1)在ORA后面加1234
    s/ORA/只匹配ORA
    [root@node1 ~]# sed 's/ORA/& 1234/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA 1234 Associates, Inc.
    
    2)在行尾加1234
    s/ORA.*/ 匹配整行
    [root@node1 ~]# sed 's/ORA.*/& 1234/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc. 1234
    
    [root@node1 ~]# sed 's/ORA.*/&1234/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.1234
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    2.6 例5
    \s \\s 
    UNIX换成\s-2UNIX\s0
    1.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    [root@node1 ~]# sed 's/UNIX/\\s-2&\\s0/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the \s-2UNIX\s0 Operating System.
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    2.7 例6
    See Section 1.4
    See Section 12.9
    将上面两行()
    1.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    [root@node1 ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    (See Section 1.4)
    (See Section 12.9)
    
    2.
    [root@node1 ~]# sed -r 's/See Section [1-9][0-9]*.[1-9][0-9]*/(&)/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    (See Section 1.4)
    (See Section 12.9)
    [root@node1 ~]# 
    
    
    See Section\fb1.4\fp
    See Section\fb12.9\fp
    3.
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    [root@node1 ~]# sed -r 's/(See Section) ([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section\fb1.4\fp
    See Section\fb12.9\fp
    [root@node1 ~]# 
    或
    [root@node1 ~]# sed -r 's/(.*) ([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section\fb1.4\fp
    See Section\fb12.9\fp
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    2.8 例7
    first:second
    one:two
    
    将上面两行调换位置
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    [root@node1 ~]# sed -r 's/(.*):(.*)/\2:\1/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    second:first
    two:one
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.9 例8
    substitution换成substitute
    
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# sed '/^\.XX /s/substitution/substitute/g' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitute command"
    [root@node1 ~]# 
    
    
    s/^\.XX \(.*\)$/\/^\\.XX \/s\/\1/\1\//
    s#^\.XX (.*)$#/^\\.XX /s\1\1#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    3. 删除 d

    3.1 例1
    [root@node1 ~]# cat -n abc 
         1  Column1 Column2 Column3 Column4
         2  .Ah "Major Heading"
         3  aAh Major Heading
         4  zAh Major Heading
         5  ORA Associates, Inc.
         6  on the UNIX Operating System.
         7  See Section 1.4
         8  See Section 12.9
         9  first:second
        10  one:two
        11  .XX "sed, substitution command"
    
    1.删除.Ah开头的
    [root@node1 ~]# sed '/.Ah/d' abc 
    Column1 Column2 Column3 Column4
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    2.删除.Ah开头的
    [root@node1 ~]# sed '/\.Ah/d' abc 
    Column1 Column2 Column3 Column4
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    4. 追加(a)插入(i)和更改©

    append 追加
    insert 插入
    change 更改
    
    语法:
    [line-address]a\
    [line-address]i\
    [address]c\
    test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    4.1 例1 追加 a
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    
    
    1.在第一行后面加abc
    [root@node1 ~]# sed '1a abc' abc 
    Column1 Column2 Column3 Column4
    abc
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    2.在第一行后面加abc ddas
    [root@node1 ~]# sed '1a abc ddas' abc 
    Column1 Column2 Column3 Column4
    abc ddas
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    3.在第一行后面加  abc
    [root@node1 ~]# sed '1a\  abc' abc 
    Column1 Column2 Column3 Column4
      abc
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    
    4.利用匹配的方式在zAh Major Heading后加  abcdes
    [root@node1 ~]# sed '/^zAh/a \  abcdes' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
      abcdes
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    5.在See后加abcdes
    [root@node1 ~]# sed '/^See/a abcdes' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    abcdes
    See Section 12.9
    abcdes
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]#
    
    
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    
    
    See Section 12.9
    See Section 13.5
    6. 上面两行后加abc
    [root@node1 ~]# sed '/See.*[1-9][0-9]\.[0-9]/a abc' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    abc
    See Section 13.5
    abc
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    4.2 例2 插入 i
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    
    1.在第一行插入mushuang
    [root@node1 ~]# sed '1i mushuang' abc 
    mushuang
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    或
    [root@node1 ~]# sed '/^\.Ah/i abc' abc 
    Column1 Column2 Column3 Column4
    abc
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    4.3 例3 更改 c
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    
    1.将ORA Associates, Inc.改成papap
    [root@node1 ~]# sed '/ORA/c papap' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    papap
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    4.4 例4 i
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    Larry' s Address
    
    1.Larry' s Address后加两行
    [root@node1 ~]# sed '/Larry/i 4700 Cross Court\
    > French Lick, IN
    > ' abc
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    4700 Cross Court
    French Lick, IN
    Larry' s Address
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    4.5 例5
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    Larry' s Address
    [root@node1 ~]# 
    
    1. .Ah开头,on结尾替换 abcd
    [root@node1 ~]# sed '/^\.Ah/,/on /c abcd' abc
    Column1 Column2 Column3 Column4
    abcd
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    Larry' s Address
    [root@node1 ~]# 
    
    
    2.全部内容替换成abcde
    [root@node1 ~]# sed '/^Column/,$c abcde' abc 
    abcde
    [root@node1 ~]# 
    或者
    [root@node1 ~]# sed '1,$c abcde' abc 
    abcde
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    5. 转换 y

    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    [root@node1 ~]# cat abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    aAh Major Heading
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    Larry' s Address
    
    1.将第三行中的ajg转换成978
    [root@node1 ~]# sed '3y/ajg/978/' abc 
    Column1 Column2 Column3 Column4
    .Ah "Major Heading"
    9Ah M97or He9din8
    zAh Major Heading
    ORA Associates, Inc.
    on the UNIX Operating System.
    See Section 1.4
    See Section 12.9
    See Section 13.5
    first:second
    one:two
    .XX "sed, substitution command"
    Larry' s Address
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    6. 打印 p

    6.1 例
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    
    1.
    [root@node1 ~]# sed '/^\.Ah/{p}' bca 
    .Ah "Comment"
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "Append, Insert and Change"
    .Ah "List"
    .Ah "List"
    [root@node1 ~]# 
    
    2.
    [root@node1 ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' bca 
    .Ah "Comment"
    Comment
    .Ah "Substitution"
    Substitution
    .Ah "Delete"
    Delete
    .Ah "Append, Insert and Change"
    Append, Insert and Change
    .Ah "List"
    List
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    7. 打印行号

    7.1 例
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    
    将第三行行号及其内容打印出来
    [root@node1 ~]# sed -n '/Delete/{=;p}' bca 
    3
    .Ah "Delete"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    8. 下一步 (next)n

    8.1 例
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    
    1. .Ah "Delete"打印下一行内容
    [root@node1 ~]# sed -n '/Delete/p' bca 
    .Ah "Delete"
    [root@node1 ~]# sed -n '/Delete/n;p' bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Append, Insert and Change"
    .Ah "List"
    [root@node1 ~]# 
    
    2. 将.Ah "Append, Insert and Change"的下一行List加个()
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    [root@node1 ~]# sed -r '/Append/{n;s/\.Ah (.*)/\.Ah (\1)/g}' bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah ("List")
    [root@node1 ~]# 
    
    3.
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    Append 123
    .Ah "Substitution"
    Append 456
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    Append 567
    
    [root@node1 ~]# sed -r '/Append/{n;s/\.Ah (.*)/\.Ah (\1)/g}' bca 
    .Ah "Comment"
    Append 123
    .Ah ("Substitution")
    Append 456
    .Ah ("Delete")
    .Ah "Append, Insert and Change"
    .Ah ("List")
    Append 567
    [root@node1 ~]# 
    
    
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    Append 123
    
    .Ah "Substitution"
    Append 456
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    
    .Ah "List"
    Append 567
    
    4. 取有Append行,中下一行有空格删除
    [root@node1 ~]# sed '/Append/{n;/^$/d}' bca 
    .Ah "Comment"
    Append 123
    .Ah "Substitution"
    Append 456
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    .Ah "List"
    Append 567
    [root@node1 ~]# 
    
    [root@node1 ~]# sed -n '/Append/{n;/^$/d};p' bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "List"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    9. 读(r)和写(w)

    9.1 例 w
    [root@node1 ~]# cat bca 
    .Ah "Comment"
    Append 123
    
    .Ah "Substitution"
    Append 456
    .Ah "Delete"
    .Ah "Append, Insert and Change"
    
    .Ah "List"
    Append 567
    
    [root@node1 ~]# sed -n '/Append/{n;/^$/d};p' bca 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "List"
    [root@node1 ~]# 
    
    [root@node1 ~]# cat 555 
    .Ah "Comment"
    .Ah "Substitution"
    .Ah "Delete"
    .Ah "List"
    [root@node1 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    Java EE-使用Servlet搭建一个简单的前后端交互程序
    Flink概念及应用场景
    零代码,让业务人员实现应用创造自由
    比 Nginx 性能更强的下一代 Web 服务器
    css3的text(文本)
    山东大学高频电子线路综合实验 调幅通信机系统实验详解
    wait/notify
    java中对象和json格式相互序列化和反序列化的函数
    最新2024年项目基金撰写与技巧及GPT融合应用
    什么是原生IP与广播IP?原生IP有何优势?
  • 原文地址:https://blog.csdn.net/mushuangpanny/article/details/126131729