2

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
  • 40,256
  • 21
  • 101
  • 138
  • 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

7

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
  • 361
  • 1
  • 3
2
echo "one two three" | sed 's/ /\n/g'
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
0

A working solution using grep and regular expressions:

echo "foo bar boo you too" | grep -oE "[a-z]+"
julienc
  • 19,087
  • 17
  • 82
  • 82
0

[in Bash] lit.

$ echo "foo bar boo you too" | ( read -a A; printf '%s\n' "${A[@]}"; )
foo
bar
boo *
you
too
konsolebox
  • 72,135
  • 12
  • 99
  • 105