V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
douglas1997
V2EX  ›  问与答

这种替换的 awk 脚本应该怎么写呢?

  •  
  •   douglas1997 · 2017-10-24 16:00:03 +08:00 · 858 次点击
    这是一个创建于 2382 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文:

    bottom: 'conv1_1/3x3'
    top: 'conv2_1/4x4'
    

    替换得到

    bottom: 'conv1_2/3x3'
    top: 'conv2_2/4x4'
    

    我写的脚本是这样的:

    awk '{if( (match($0, "bottom") && match($0, "_1/")) or (match($0, "top") && match($0, "_1/")) ) {print $0 "\n" gsub("\_1\/", "\_2\/");} else {print "1";} }' step1.prototxt
    

    但是最后的结果不太对,有很多的零,很奇怪这样的结果。哪里不对,或者朋友们有没有更简单明了的写法。

    1 条回复    2017-10-24 16:20:23 +08:00
    czheo
        1
    czheo  
       2017-10-24 16:20:23 +08:00
    >> awk '{gsub("_.", "_2", $0); print $0}' test.txt
    但是 sed 显然比较方便啊
    >> sed 's/_./_2/' test.txt
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   752 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:45 · PVG 05:45 · LAX 14:45 · JFK 17:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.