[How To] Replace all carriage-returns with spaces in text file

[Unix] Replace all carriage-returns with spaces in text file

Thus spake the master, 'unless absolutely necessary, it is not suggestible to roam into the dark woods of sed and awk'

There are simpler ways of doings things in Unix, one simply should be aware of them. One such simple trick to replace all newline feeds (carriage returns) with spaces is to use the simple cat command. Suppose you have the following file with random names in each line and the requirement is to remove all the newline feeds with spaces to get all names in one file.

This can be done as follows with the help of $(cat filename) command.

$ cat myfile
mango
tomato
sitara
sagupta
vishal
saylee
shweta
dolly
shilpi
ritu
malvika
bash-3.00$ echo $(cat myfile)
mango tomato sitara sagupta vishal saylee shweta dolly shilpi ritu malvika


click on the image to enlarge