Append to the previous line for a match

后端 未结 4 1220
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 09:34

can I use sed or awk to append to the previous line if a match is found ?

I have a file which has the format :

          INT32         


        
4条回答
  •  逝去的感伤
    2021-01-23 09:42

    This might work for you (GNU sed):

    sed '$!N;s/\n\s*{\s*$/{/;P;D' file
    

    Explanation:

    • $!N unless the last line append the next line to the pattern space.
    • s/\n\s*{\s*$/{/ replace a linefeed followed by no or any amount of white space followed by an opening curly brace followed by no or any amount of white space to the end of the string, by an opening curly brace.
    • P print upto and including the first newline.
    • D delete upto and including the first newline (if so do not start a new cycle).

提交回复
热议问题