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