1

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 ?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) that helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.