remove new line if next line does not begin with a number

前端 未结 4 1725
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 16:54

I have a file that is like

    1 test
    test

How can I remove the new line from the so the final output becomes:

1 test test

4条回答
  •  隐瞒了意图╮
    2021-01-19 17:48

    You can be a bit smarter and print a new line before the line if it starts with a digit (except for the first line);

    awk 'BEGIN{ORS="";} NR==1 { print; next; } /^[[:digit:]]/ { print "\n"; print; next; } { print; }'
    

    The awk script:

    BEGIN { ORS=""; }                            # default: no newline between output records
    NR==1 { print; next; }                       # first line: print
    /^[[:digit:]]/ { print "\n"; print; next; }  # if starts with a digit: print newline before
    {print;}                                     # other lines (next; has not been called yet)
    

提交回复
热议问题