Have sed make substitute on string but SKIP first occurrence

前端 未结 3 1008
耶瑟儿~
耶瑟儿~ 2020-12-17 15:52

I have been through the sed one liners but am still having trouble with my goal. I want to substitue matching strings on all but the first occurrence of a line. My exact usa

相关标签:
3条回答
  • 2020-12-17 15:54

    You can avoid the problem with g and n

    Replace all of them, then undo the first one:

    sed -e 's/ /\\ /g' -e 's/\\ / /1'
    

    Here's another method which uses the t branch-if-substituted command:

    sed ':a;s/\([^ ]* .*[^\\]\) \(.*\)/\1\\ \2/;ta'
    

    which has the advantage of leaving existing backslash-space sequences in the input intact.

    0 讨论(0)
  • 2020-12-17 16:06
    s/ /\\ /2g
    

    The 2 specifies that the second one should apply, and the g specifies that all the rest should apply too. (This probably only works on GNU sed. According to the Open Group Base Specification, "If both g and n are specified, the results are unspecified.")

    0 讨论(0)
  • 2020-12-17 16:15

    use awk

    $ echo cd 'blah blah/thing/another space/' | awk '{for(i=2;i<NF;i++) $i=$i"\\"}1'
    cd blah\ blah/thing/another\ space/
    
    $ echo 'cd /Users/joeuser/bump bonding/initial trials' | awk '{for(i=2;i<NF;i++) $i=$i"\\"}1'
    cd /Users/joeuser/bump\ bonding/initial\ trials
    
    0 讨论(0)
提交回复
热议问题