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

前端 未结 4 1731
没有蜡笔的小新
没有蜡笔的小新 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:41

    A solution using 'sed':

    Input file (infile):

    1 test
    test
    2 two 
    3 three
    4 four
    five 
    six
    7 seven
    eight
    9 nine
    

    'Sed' program (script.sed):

    /[0-9]\+/ {
            : a 
            N
            /\n[0-9]\+/ {
                    P
                    s/.*\n//
            }
            t a
    }
    
    y/\n/ /
    

    Execution:

    $ sed -f script.sed infile
    

    Output:

    1 test test
    2 two
    3 three
    4 four five 
    six
    7 seven eight
    9 nine
    

提交回复
热议问题