zoukankan      html  css  js  c++  java
  • sed命令案例

    高级sed命令案例

    处理多行空间模式(N,D,P等)

    N(追加下一行)

    [root@node0 ~]# cat list 
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    
    [root@node0 ~]# sed -n '/^John/{N;s/MA
    Alice/conntect/p}' list #对于寻址的多次操作:N取匹配的下一行,替换换行合成一行
    John Daggett, 341 King Road, Plymouth conntect Ford, 22 East Broadway, Richmond VA
    
    [root@node0 ~]# sed -n  '/^John/{N;s/22/da/p}' list #通过N取到下一行只对下一行进行操作替换22为da
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, da East Broadway, Richmond VA
    
    

    $!N(多行模式空间中的最后一行不处理)

    [root@node0 ~]# cat a
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    avaliable on your system.
    [root@node0 ~]# sed -n '/section/{$!N;p}' a #这里可以看到打印时不包含最后一行
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    consult section 3.1 in the owner and operator
    guide for a description of the tape drives
    
    [root@node0 ~]# sed '$!N;s/operator
    guide/installation/g' a #在不需要匹配特定行时直接使用$!N,最后一行不会处理并输出
    consult section 3.1 in the owner and installation for a description of the tape drives
    consult section 3.1 in the owner and installation for a description of the tape drives
    consult section 3.1 in the owner and installation for a description of the tape drives
    avaliable on your system.
    
    [root@node0 ~]# cat a
    <para>
    this is a test
    <fiugre begin>
    111001110010101000111100
    <figure end>
    <para>
    more lines shouold be found and print
    [root@node0 ~]# sed '/para/{
    N
    c
    .LP
    }
    /<figure begin>/,/<figure end>/{
    w fig.interleaf
    /<fiugre end>/i
    .FG
    <insert figure here>
    .FE
    d
    }' a
    .LP
    <fiugre begin>
    111001110010101000111100
    <figure end>
    .LP
    
    

    D多行删除

    //#n文本不显示,方便识别
    [root@node0 ~]# cat a
    1 blank line
    #1
    2 blank line
    #2
    #3
    3 blank line
    #4
    #5
    #6
    4 blank line
    #7
    #8
    #9
    #10
    line end
    [root@node0 ~]# sed '/^$/{N;/^
    $/d}' a #匹配空行,N加入下一行,匹配换行符空格删除。#1下一行是“2blankline”,改行不为空,不处理。#2#3都有换行符和空格,d删除模式空间所有的内容
    1 blank line
    
    2 blank line
    3 blank line
    
    4 blank line
    line end
    
    [root@node0 ~]# sed '/^$/{N;/^
    $/D}' a #匹配奇数空行,两行都输出,偶数空行,则都删除
    1 blank line
    
    2 blank line
    
    3 blank line
    
    4 blank line
    
    line end
    
    
    

    多行打印P(输出模式空间第一行到换行符为止)

    [root@node0 ~]# cat list
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Eric Adams, 20 Post ROad, Sudbury MA
    Hubert Sims, 328A Brook Road, Roanoke VA
    Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston MA
    
    [root@node0 ~]# sed -n '/John/{N;p}' list #p打印模式空间所有的内容
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    
    [root@node0 ~]# sed -n '/John/{N;P}' list #P打印模式空间第一行到换行为止
    John Daggett, 341 King Road, Plymouth MA
    
    
    //多行打印实现的循环(行首行尾对应时)
    [root@node0 ~]# cat a
    here are the example of unix
    system. where unix
    system appears.it should be the unix
    operating system.
    [root@node0 ~]# sed '/unix$/{ #匹配unix结尾的行
    N   #下一行
    /
    system/{   #匹配行交界处
    s// operating &/   #替换operating和匹配内容
    P  #打印替换内容
    D #删除模式空间第一行内容
    }}' a
    here are the example of unix operating 
    system. where unix operating 
    system appears.it should be the unix
    operating system.
    
    

    包含该行

    • 模式空间: 处理输入行的缓冲区
    • 保持空间:作为临时储存,通过命令可以让模式空间的内容相互交换
    命令 缩写 功能
    Hold h,H 模式空间->保持空间(复制,追加)
    Get g,G 保持空间->模式空间(复制,追加)
    Exchange x 交换保持空间和模式空间的内容

    基于空间交换达成的行间换位

    [root@node0 ~]# cat a
    1
    2
    11
    22
    111
    222
    1111
    2222
    
    [root@node0 ~]# sed  '
    /[1].*/{
    h #匹配只出现含有1的任意次数的行存入保持空间
    d #删除模式空间的内容1,11,111,1111,此时模式空间为空
    }
    /[2].*/{ #匹配只含有2的数字按匹配顺序放入模式空间
    G   #取出保持空间的内容按顺序追加到匹配只含有2的任意次数的行后面
    }
    ' a
    2
    1
    22
    11
    222
    111
    2222
    1111
    
    [root@node0 ~]# sed  '
    /[1].*/{
    h #匹配只出现含有1的任意次数的行存入保持空间
    d #删除模式空间的内容1,11,111,1111,此时模式空间为空
    }
    /[2].*/{
    x   #交换模式空间和保持空间的内容,此时2.22.222.2222在保持空间
    }' a
    1
    11
    111
    1111
    

    基于替换实现的文本块构建

    [root@node0 ~]# cat a
    this is a test
    
    this is a test
    
    this is a test
    
    this is a test
    
    this is a test
    
    [root@node0 ~]# sed '/^$/!{ #对所有非空行匹配
    H #匹配行保存至保持空间
    d  #删除模式空间内容
    }
    /^$/{ #匹配空行
    x #交换保持空间内容,此时非空行在空行存在位置
    s/^
    /<p>/ #替换行首换行符为文本块头
    s/$/</p>/ #替换行首换行符为文本块尾
    G #追加保持空间的空白行到替换成功的非空行后面
    }' a
    <p>this is a test</p>
    
    <p>this is a test</p>
    
    <p>this is a test</p>
    
    <p>this is a test</p>
    
    
    
  • 相关阅读:
    单调栈
    P1164 小A点菜
    P1156 垃圾陷阱
    P1140 相似基因
    P1136 迎接仪式
    P1133 教主的花园
    P1131 [ZJOI2007]时态同步
    P1130 红牌
    利用SQLite在android上实现增删改查
    利用SQLite在android上创建数据库
  • 原文地址:https://www.cnblogs.com/fangxinxin/p/14595101.html
Copyright © 2011-2022 走看看