5

How to iterate file which has comma separated by value ?

I tried the following:

$ cat file | tr ','  '\n' > /tmp/f1
$ while read -r line;do 
   echo $line;
done < /tmp/f1

How can i iterate over the first line contents without the creation of temporary file?

Any ideas?

Syed Tahoor Ali
  • 93
  • 1
  • 2
  • 5
  • It does not make sense to iterate a file to just echo $line (the iteration must imply something more). Otherwise, for just printing lines: tr',''\n' <file would be enough – RomanPerekhrest Nov 28 '19 at 10:43
  • It looks like you've already written some code to do this -- in what way does it not do what you need? If there's an error message, or any other problem in the output, please include that. – JigglyNaga Nov 28 '19 at 10:50

2 Answers2

21

First of all, avoid doing text-parsing with shell loops. It is hard to do, easy to get wrong and very hard to read. And slow. Very, very, slow. Instead, use something like awk which is specifically designed to read by "fields". For example, with this input file:

foo, bar, baz
oof, rab, zab

You can read each comma-separated field using awk -F, to set the field separator to ,:

$ awk -F, '{ print "The 1st field is",$1,"the 2nd", $2,"and the 3rd", $3}' file
The 1st field is foo the 2nd  bar and the 3rd  baz
The 1st field is oof the 2nd  rab and the 3rd  zab

Even if you insist on doing it in the shell, you don't need the temp file and you don't need tr. You can tell while read to separate on commas:

$ while IFS=, read -r one two three; do 
    echo "The 1st field is $one, the 2nd $two and the 3rd $three"; 
  done < file
The 1st field is foo, the 2nd  bar and the 3rd  baz
The 1st field is oof, the 2nd  rab and the 3rd  zab
terdon
  • 242,166
3

Fields in a csv file could span several lines, for this reason and others, it's why I preferred to use xsv when I had to parse csv.

One way to parse a csv file with bash and xsv can be :

csvFile="myfile.csv"
lengthItems=$((($(xsv count "$csvFile") - 1 ))) # -1 because for loop start at 0

for i in $( seq 0 "$lengthItems" ); do

row=&quot;$(xsv slice -i &quot;$i&quot; &quot;$csvFile&quot;)&quot; # real magic happening here

# Do what you want with your $row here  

done

learner
  • 161