0

I have a simple string:

 foo bar boo you too

I'm looking for an easy way in bash on the command line (not in a script) to take this string in via a pipe or input redirection and convert it to

 foo
 bar
 boo
 you
 too
Ray
  • 38,399
  • 19
  • 91
  • 131
  • 2
    See [this](http://stackoverflow.com/questions/1853009/replace-all-whitespace-with-a-line-break-paragraph-mark-to-make-a-word-list). i.e. `tr ' ' '\n' <<< "$string"` (or use one of the better multi-space solutions from the post) – Reinstate Monica Please Jul 11 '14 at 18:50

4 Answers4

5

You could use 'tr' to translate the space to a new line:

~$ echo 'foo bar boo you too' | tr ' ' '\n'
foo
bar
boo
you
too
Andrew
  • 341
  • 1
  • 3
1
echo "one two three" | sed 's/ /\n/g'
FuzzyTree
  • 31,024
  • 3
  • 51
  • 80
0

A working solution using grep and regular expressions:

echo "foo bar boo you too" | grep -oE "[a-z]+"
julienc
  • 17,267
  • 17
  • 78
  • 79
0

[in Bash] lit.

$ echo "foo bar boo you too" | ( read -a A; printf '%s\n' "${A[@]}"; )
foo
bar
boo *
you
too
konsolebox
  • 66,700
  • 11
  • 93
  • 101