I want to calculate the sum of a specific column using bash without using the print that specific column (I want to keep all the output columns of my pipeline and only sum one of them!)
“UtilityBills. txt” represents the name of the text file from which we have to read the data. Then we have the “awk” keyword followed by the “sum” expression that will actually calculate the sum from the second column of our dataset, and then the “print” command will be used to display the results on the terminal.
If you wanted to sum over, say, the second column, but print all columns in some pipeline:
cat data | awk '{sum+=$2 ; print $0} END{print "sum=",sum}'
If the file data looks like:
1 2 3
4 5 6
7 8 9
Then the output would be:
1 2 3
4 5 6
7 8 9
sum= 15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With