0

I have this table below, how can I calculate the sum of bytes for records in Nov only? for example below I want to find rows in Nov then sum the numbers in column4 relevant to Nov only? how I can do it?

1 arnold   user   1933 Nov  7 13:05 
2 megan   user  10809 Nov  7 13:03 
3 sam   user    983 Apr 13 12:14 
4 mark   user  31869 Jun 15 12:20 
5 sandy   user  22414 Nov  7 13:03 
6 semon   user  37455 Nov  7 13:03 
7 andre   user  27511 Dec  9 13:07 
8 jim   user   7989 Nov  7 13:03 
3

You can try awk as follows:

awk '$5 == "Nov" { sum += $4 }END { print sum }'  file
80600

$5 represent the column relevant to months. $5 == "Nov" will filter the table for all records in November, then awk will sum the numbers in column $4

2

Using awk:

awk 'BEGIN {sum = 0}
$5 == "Nov" {sum += $4}
END {print sum}' < data

where data is your file containing the record.

0

To enable you to change the target month simply, try summing all months in an array, and print the relevant array element only:

awk '{SUM[$5] += $4} END {print SUM["Nov"]}' file
80600

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.