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!)
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Sum multidimensional array C#
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
If you wanted to sum over, say, the second column, but print all columns in some pipeline:
If the file data looks like:
Then the output would be:
Assuming the same input data as @John1024, you can use good ol'
cut
andpaste
and some bash arithmetic: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
cat
ing the input data for illustrative purposes - it could be piped from another source, or the data file passed directly tocut
as well.Do you want to continuously sum one column, step by step?
Does is have to be
bash
or can you useawk
:Given a file like:
Total of 1st Column is
15
.