0

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?

  • 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
4

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.