Reputation: 97
As far as I understand, "while read" is so slow, because it reads byte-wise. Is there a more efficient way to do this if line-wise is sufficient for me?
I have a file data.txt containing four parameters per line:
1 56 56 48
3 646 86 656
4 56 894 959
6 89 849 875
etc.
and i want to perform an operation on each line (assign each value to a variable for further processing).
this is how i do it now:
cat data.txt |
while read linewise; do
par0=`echo $linewise |awk '{print $1}'`;
par1=`echo $linewise |awk '{print $2}'`;
par2=`echo $linewise |awk '{print $3}'`;
par3=`echo $linewise |awk '{print $4}'`;
echo $par0 $par1 $par2 $par3
done
But for a ~1000 line file this takes half a minute:
real 0m30.380s
user 0m7.996s
sys 0m11.820s
How can I speed this up?
Upvotes: 2
Views: 1312
Reputation: 22821
You could speed it up by removing all the pipes and calls to subshells. The below greatly simplifies what you're doing:
while read -r par0 par1 par2 par3; do
echo $par0 $par1 $par2 $par3
done < data.txt
Upvotes: 5