How to replace spaces with newlines/enter in a text-file?
A few choices:
The classic, use
tr
:tr ' ' '\n' < example
Use
cut
cut -d ' ' --output-delimiter=$'\n' -f 1- example
Use
sed
sed 's/ /\n/g' example
Use
perl
perl -pe 's/ /\n/g' example
Use the shell
foo=$(cat example); echo -e ${foo// /\\n}
Try the below command
awk -v RS=" " '{print}' file
OR
awk -v RS='[\n ]' '{print}' file
Example:
$ awk -v RS=" " '{print}' example
abc
cdef
ghi
jk
lmnopq
rst
uv
wxyz
Explanation:
RS (Record seperator) is an built-in awk variable. In the first command, the value given to the Record separator variable is space. so awk breaks the line from printing whenever it finds a space.
In the second command, the value given to the RS variable is space or a new line character.This command eliminates the extra blank line appeared while running the first command.
You can use xargs
,
cat example | xargs -n 1
or, better
xargs -n 1 < example