Using awk (or sed) to remove newlines based on first character of next line

前端 未结 5 2053
陌清茗
陌清茗 2021-02-15 14:23

here\'s my situation: I had a big text file that I wanted to pull certain information from. I used sed to pull all the relevant information based on regexp\'s, but each \"piece\

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 14:51

    Without special-casing field 3, easy.

    awk '
        !/^,/   { if (NR > 1) print x ; x = $0 }
        /^,/    { x = x OFS $0 }
        END     { if (NR) print x }
    '
    

    With, more complex but still not too hard.

    awk '
        !/^,/   { if (n && n < 3) print x ; x = $0 ; n = 1 }
        /^,/    { if (++n > 2) { print x, $0 } else { x = x OFS $0 } }
        END     { if (n && n < 3) print x }
    '
    

提交回复
热议问题