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
I already tried some sed but I could not het it to work.
This should do the trick:
sed -n '$!{ 1{x;d}; H}; ${ H;x;s|\n\([^0-9]\)| \1|g;p}' inputfile
Input:
1 test1
test1
2 test2
test2
test2
3 test3
4 test4
Output:
1 test1 test1
2 test2 test2 test2
3 test3
4 test4
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With