Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed: Replace FIRST occurence of space with newline

I am trying to replace the first occurrence of a space in each line with a newline.

For instance

123 there is a monkey
567 there is also a tiger

would become

123
there is a monkey
567
there is also a tiger

I have tried doing this with the command:

sed -e 's/^\s\+/\n/g' output.txt > new.txt

with no luck. I am also open to any other programs in linux that would allow me to do this such as awk, perl, or bash.

like image 599
beckah Avatar asked Dec 12 '25 16:12

beckah


1 Answers

You can use:

sed $'s/ \{1,\}/\\\n/' file
123
there is a monkey
567
there is also a tiger

This works both on BSD as well on gnu sed.

like image 200
anubhava Avatar answered Dec 15 '25 17:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!