I need to find a way to sum up all the integer values of a specific column in a file, and print its result. This is a piece of my file:
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 1 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 2 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 2 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 0 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
<<< The program found 1 rare variants for the gene ARIH1 for this HEALTHY_CONTROL <<<
I want to print the sum of the 5th column, that is the total number of rare variants. In this example, it should print 6.
I tried the following command (which did not work):
grep "rare variants for the gene ARIH1" fileName | tail -n+2 | awk -F " " '{sum+=$5} END {print sum}'
This command prints 1, which is wrong.
How can I do? Thanks!
awk '/rare variants for the gene ARIH1/{sum += ($5 + 0)} END{print sum}' filenameshould do the job.grep "rare variants for the gene ARIH1" fileName | tail -n+2? and why did you add this part in your question?awk '/rare variants for the gene ARIH1/{print $5}' file