convert comma separated values into a list of values using shell script

14,119

Solution 1

Use tr to change , into newlines:

tr , "\n" < list.txt

See https://en.wikipedia.org/wiki/Tr_(Unix)

Solution 2

You could use the tr command to transform each "," into a newline.

cat list.txt | tr "," "\n"

From then you can output each line wherever you want using a while read loop

cat list.txt | tr "," "\n" | while read LINE
 do
  echo "$LINE" >> ~/wherever/you/want
done

Or ...

while read LINE
 do
  echo "$LINE" >> ~/wherever/you/want
done <<< "$(cat list.txt | tr "," "\n")"

Either works.

Solution 3

You can use sed for this:

sed "s/,/\n/g" list.txt

Output

a
b
c
d
e
f

Solution 4

Yes, you can use cut. The key point is the usage of --output-delimiter. If you set it as new line, everything works:

cut -d',' --output-delimiter=$'\n' -f1- file

Note also two facts:

  • we use -f1- to print from the first field up to the last one. The n- syntax means: from the n-th field up to the end.
  • we use $'\n' because \n alone would print literal \n instead of real new lines.

Test

$ echo "a,b,c,d,e,f,g,h" | cut -d',' --output-delimiter=$'\n' -f1-
a
b
c
d
e
f
g
h

From man cut:

--output-delimiter=STRING

use STRING as the output delimiter the default is to use the input delimiter

Share:
14,119
Prashanth
Author by

Prashanth

Updated on June 24, 2022

Comments

  • Prashanth
    Prashanth almost 2 years

    I have csv data like:

    a,b,c,d,e,f,g,h
    

    I want this data in a list, one letter in each line. Is it possible is shell script? I have tried to extract each letter using cut command as shown below:

    less list.txt | cut -d"," -f?
    

    The command is working but the problem here is the number of fields is unknown. I have to place this command in a loop and iterate on the list so that I can extract the values one-by-one a redirect them to another file.

    • DavidDomain
      DavidDomain almost 9 years
      You should post some code and point to with what you are have problems, this will make it a lot easier for other to help you solve it.
    • Steve Summit
      Steve Summit almost 9 years
      Side point: it's odd to pipe your file into your command using less. More conventional would be cut file, cut < file, or, if you really want to use a pipe, cat file | cut. If less did its usual job, you'd have to keep hitting the spacebar to finish, but actually, it notices that standard output is not your screen, and basically turns itself into cat for you.
  • Armfoot
    Armfoot almost 9 years
  • fedorqui
    fedorqui almost 9 years
    @Armfoot thanks, even though it is something I am already posting in my answer. That is, I don't see what value this is adding. But thanks for your support
  • jhoepken
    jhoepken almost 9 years
    Yes, I know. But it doesn't do harm, does it?
  • Armfoot
    Armfoot almost 9 years
    ah... it's just a real test/fiddle/confirmation is much nicer to the one who's going to use it ;)
  • 123
    123 almost 9 years
    neither does writing sed -r -s -u -l10000 --posix -e "s/,/\n/g". Still pointless though.
  • Etan Reisner
    Etan Reisner almost 9 years
    The value in using -e is that if the sed script ever needs to start with a - (which may or may not actually be possible) you won't need to remember to add -e and if you ever want to add a second script to the command you can just add -e <script> to the end without needing to go add -e before the first script. That said it doesn't really matter.
  • 123
    123 almost 9 years
    @EtanReisner I'm pretty sure it would be a syntax error if you started with a -. My point was that it is a single script that doesn't begin like that. I understand when -e can be useful, just like all the flags in my other comment "can" be useful, just not in this case.
  • 123
    123 almost 9 years
    @EtanReisner Yes, because that is it's purpose, obviously the other flags aren't going to do that, they are all useful though in situations that demand them. It's like saying -r should be there so you don't have to remember to add it if you start using ERE. But OP is not and neither of them is needed.