I've to make a script which will be able to sum the columns. The file looks like that:
1 2 3
1 2
1
While executing script without the argument ./sum.sh I get the sum of all the columns so the answer is 10. But I have to make it add only certain columns. For example ./sum.sh 1 3 should sum first and third column and give 6.
My code:
sum=0
if [[ $# -eq 0 ]]; then
awk '{for(i=1;i<=NF;i++) sum+=$i;}; END {print sum}' plik.txt
else
exit 0;
fi
while [ $# -gt 0 ]
do
awk '{sum +="$1"} END {print sum}' plik.txt
shift
done
I think that I'm quite close to the solution, but something must be missing.
awk: stackoverflow.com/questions/33006845/… , If you are planning on doing such thing it might be better to letawkdetermine if there's one argument or more per line.