Turning a list items on lines into a horizontal list

I just realised I can use xargs to quickly turn a list of items into a horizontal list for further use on the command line.  Well of course that’s the fundamental purpose of xargs, but with echo I can do it without having xargs execute the actual command in question.

i.e.


cat my.csv | cut -d, -f 2 | xargs echo

Can take a column from a csv and turn it into a horizontal list, like,


12354
32132
77632
09211
99321

to


12354 32132 77632 09211 99321

Kind of a no brainer when you think about it, if only I thought of it more often.

One thought on “Turning a list items on lines into a horizontal list

  1. I’m always using shell to flatten lists 🙂

    $ echo `cut -d, -f 2 my.csv`

    another common idiom…

    $ for i in `cut -d, -f 2 my.csv`; do stuff $i; done

Leave a comment