Reputation: 613
I have a bash file say input.dat which looks like following.
1 2 4 6
2 3 6 9
3 4 8 12
I want the data in 2nd, 3rd and 4th column to be added and printed in output.dat file like following
1 12
2 18
3 24
How can this be achieved in bash ?
Upvotes: 1
Views: 44
Reputation: 10129
Using awk you can do this:
awk '{print $1, $2+$3+$4}' input.dat
and if you prefer bash it can be done like this (at least if the numbers are integers): bash sum.sh < input.dat
and sum.sh is
sum.sh
while read -r v1 v2 v3 v4;
do
echo $v1 $(( v2 + v3 + v4 ))
done
Upvotes: 4