sed: Find pattern over two lines, not replace after that pattern

前端 未结 3 1495
Happy的楠姐
Happy的楠姐 2021-01-21 09:22

Wow, this one has really got me. Gonna need some tricky sed skill here I think. Here is the output value of command text I\'m trying to replace:

...
fast
              


        
相关标签:
3条回答
  • 2021-01-21 09:58

    Try this:

    sed "h; :b; \$b ; N; /^${1}\n     n/ {h;x;s//Noun\n/; bb}; \$b ; P; D"
    

    Unfortunately, Paul's answer reads the whole file in which makes any additional processing you might want to do difficult. This version reads the lines in pairs.

    By enclosing the sed script in double quotes instead of single quotes, you can include shell variables such as positional parameters. I would recommend surrounding them with curly braces so they are set apart from the adjacent characters. When using double quotes, you'll have to be careful of the shell wanting to do its various expansions. In this example, I've escaped the dollar signs that signify the last line of the input file for the branch commands. Otherwise the shell will try to substitute the value of a variable $b which is likely to be null thus making sed unhappy.

    Another technique would be to use single quotes and close and open them each time you have a shell variable:

    sed 'h; :b; $b ; N; /^'${1}'\n     n/ {h;x;s//Noun\n/; bb}; $b ; P; D'
    #   ↑open        close↑    ↑open                                close↑
    

    I'm assuming that the "[/code]" in your expected result is a typo. Let me know if it's not.

    0 讨论(0)
  • 2021-01-21 10:01

    This might work for you:

    sed '$!N;s/^fast\n\s*n :/Noun\n :/;P;D' file
    ...
    Noun
     : abstaining from food
    
    0 讨论(0)
  • 2021-01-21 10:13

    This seems to do what you want:

    sed -e ':a;N;$!ba;s/fast\n     n/Noun\n/'
    

    I essentially stole the answer from here.

    0 讨论(0)
提交回复
热议问题