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!)
4 Answers
Assuming the same input data as @John1024, you can use good ol' cut and paste and some bash arithmetic:
$ cat data | echo $(( $( cut -d' ' -f2 | paste -s -d+ - ) ))
15
$
The trick here is to tell paste to insert + as a delimiter, then perform bash arithmetic using $(( )) on the resulting expression.
Note I am just cating the input data for illustrative purposes - it could be piped from another source, or the data file passed directly to cut as well.