Shell Script(sed)多行匹配问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
funagi
V2EX    Bash

Shell Script(sed)多行匹配问题

  •  
  •   funagi 2017-04-16 17:51:37 +08:00 2940 次点击
    这是一个创建于 3149 天前的主题,其中的信息可能已经有所发展或是发生改变。
    div class="markdown_body">

    hosts 文件内容

    127.0.0.1 localhost 127.0.1.1 exmaple-some.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

    使用脚本

    add-host() { sed -i "/^# The following/i\\${1}" hosts } add-host '127.0.1.1 a.newdomain.com' add-host '127.0.1.1 b.newdomain.com' 

    期望结果

    127.0.0.1 localhost 127.0.1.1 exmaple-some.com 127.0.1.1 a.newdomain.com 127.0.1.1 b.newdomain.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

    实际结果

    127.0.0.1 localhost 127.0.1.1 exmaple-some.com 127.0.1.1 a.newdomain.com 127.0.1.1 b.newdomain.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

    exmaple-some.com 所在行实际内容不定,所以无法匹配这行再往下插入。

    sed 一般是对行进行处理,所以无法匹配\n# The following,查了资料说可以用跳转标记匹配多行,但我一脸懵逼没看懂用法。

    第 1 条附言    2017-04-16 19:30:05 +08:00
    能用 sed 解决当然是最好的,不用 sed 也可以,只要是 Shell Script 就行。
    8 条回复    2017-04-19 22:34:56 +08:00
    fangxing204
        1
    fangxing204  
       2017-04-16 18:31:46 +08:00 via Android
    sed 是面向“行”的编辑器
    yangg
        2
    yangg  
       2017-04-16 19:33:46 +08:00   1
    sed 专门干这事的!!!
    使用函数 a
    sed "/match_reg/a added content" -i hosts

    参考: https://uedsky.com/2016-10/sed-awk/#sed-tutorial
    funagi
        3
    funagi  
    OP
       2017-04-17 19:15:28 +08:00
    @yangg 函数 a 用法看懂了,但好像没法实现我想要的效果
    yangg
        4
    yangg  
       2017-04-17 19:51:05 +08:00 via iPhone
    不是在行下面插入内容么?什么系统?
    funagi
        5
    funagi  
    OP
       2017-04-17 20:38:44 +08:00
    @yangg Debian ,是在行上面插内容,而且还是跨行的。
    yangg data-uid=
        6
    yangg  
       2017-04-17 21:06:05 +08:00 via iPhone
    跨行什么意思,示例没看出来
    funagi
        7
    funagi  
    OP
       2017-04-17 21:20:44 +08:00
    @yangg “# The following lines are desirable for IPv6 capable hosts ”这一行上面不是有一空行吗,将这两行视为一个整体块区,在这块区的上面插入内容
    funagi
        8
    funagi  
    OP
       2017-04-19 22:34:56 +08:00
    @yangg 重新看了你提供的参考,用`sed 'N;/# The following/i content' hosts`基本上解决了问题,虽然没法确认 N 是不是空行 ,非常感谢。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2493 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 04:36 PVG 12:36 LAX 20:36 JFK 23:36
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86