V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
douglas1997
V2EX  ›  问与答

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

  •  
  •   douglas1997 · Oct 24, 2017 · 1145 views
    This topic created in 3113 days ago, the information mentioned may be changed or developed.

    原文:

    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 replies    2017-10-24 16:20:23 +08:00
    czheo
        1
    czheo  
       Oct 24, 2017
    >> awk '{gsub("_.", "_2", $0); print $0}' test.txt
    但是 sed 显然比较方便啊
    >> sed 's/_./_2/' test.txt
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1032 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 17:56 · PVG 01:56 · LAX 10:56 · JFK 13:56
    ♥ Do have faith in what you're doing.