Append to the previous line for a match

后端 未结 4 1216
隐瞒了意图╮
隐瞒了意图╮ 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:50

    One way using awk:

    awk '!(NF == 1 && $1 == "{") { if (line) print line; line = $0; next; } { sub(/^[ \t]+/, "", $0); line = line $0; } END { print line }' file.txt
    

    Or broken out on multiple lines:

    !(NF == 1 && $1 == "{") { 
        if (line) print line
        line = $0
        next
    }
    
    {
        sub(/^[ \t]+/, "", $0)
        line = line $0
    }
    
    END {
        print line
    }
    

    Results:

    INT32
              FSHL (const TP  Buffer){
              INT32
              FSHL_lm (const TP  Buffer) 
              { WORD32 ugo = 0; ...
    

    HTH

提交回复
热议问题