LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   nawk question - addition of a column (https://www.linuxquestions.org/questions/programming-9/nawk-question-addition-of-a-column-622264/)

dazdaz 02-19-2008 10:28 AM

nawk question - addition of a column
 
Hi,

I have a file, like the following, where I would like to addition up column 5 (length).

DISK DEVICE TAG OFFSET LENGTH FLAGS
ARG01-05_CU0212 c3t110d2s2 c3t110d2 25186048 2076928 -
ARG03-15_CU0561 c3t110d0s2 c3t110d0 16777216 4171520 -
ARG03-15_CU0562 c3t110d1s2 c3t110d1 4194304 16754432 -


cat out | nawk -F" " '/[0-9]+/ { sum += $5 ; print $sum}'

Above is the nawk that I am trying. Firstly check it's a number, and then addition up column 5. I read a few random web-pages, and I thought this would work, but does'nt...

I am using nawk on Solaris although I think that nawk exists on many Linux distributions too.

Many thanks for any tips.

jim mcnamara 02-19-2008 11:47 AM

Code:

nawk ' /-$/ {sum+=$5} END{ printf("%12.0f\n", sum)} ' file
23002880



All times are GMT -5. The time now is 08:50 AM.