Convert new line to space by using sed command

This is a one line sed command trick in which we will convert the new line into space.
I have a file in which there is a words per line.Now here I want to remove all these lines and instead of this I would like to use spaces.

For eg. I have a file called test and it has data as given below

linux@tuxworld:/tmp$ cat test 
red
blue
green
yellow
white
black
brown
orange
linux@tuxworld:/tmp$ 

Now using sed comand to convert new line into space

sed ':a;N;$!ba;s/n/ /g' filename 

Below is reference

linux@tuxworld:/tmp$ sed ':a;N;$!ba;s/n/ /g' test 
red blue green yellow white black brown orange
linux@tuxworld:/tmp$

If you use -i flag with sed command it will do changes in file also

linux@tuxworld:/tmp$ sed -i ':a;N;$!ba;s/n/ /g' test 
linux@tuxworld:/tmp$ cat test 
red blue green yellow white black brown orange
linux@tuxworld:/tmp$

1 thought on “Convert new line to space by using sed command”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.