I have text file which contains many words (strings) separated by a space. How can I replace the spaces by newlines. In other words, how can I have each string on a different line in bash? I would be really grateful if one could also suggest a method using sed!
5 Answers
A file bla:
a b c d e Using sed:
sed 's/\s\+/\n/g' bla Results in:
a b c d e Use the command:
sed -i.bak -e 's/\s\+/\n/g' file \s will match any whitespace character (spaces, tabs, newlines), and \+ will match on one or more occurrences in a row. -i.bak will backup your original file to file.bak.
$ cat foo a b c 1 2 3 In Gnu awk (and mawk):
$ awk -v RS=" +|\n" '{print $0}' foo a b c 1 2 3 Using tr for a single line:
$ tr -s \ '\n' <<< "a b c" a b c 2 Comments
Armin
Thank you James and sorry for repeating the questions, I had googled my question but the other question was replacing coma with newline, and well google could not help me finding the previous post.
Armin
I am actually really intrigued that your suggestion is using awk. I could never thought of awk for this problem.