简体   繁体   中英

Adding column result based on awk

I am trying to sum the result of this query which takes the HTTP error codes from access.log and sets out the total occurrences for each code:

awk '{print $9}' access.log | sort | uniq -c | sort -rn 

Result:

234 200
3   500
102 404
26 304
3 404

Trying to figure out what's the best way to sum up the result (column 1 = 234+3+102+26+3)

Thanks.

Pipe it to another awk command that prints the lines and calculates the total.

awk '{print $9}' access.log | sort | uniq -c | sort -rn | awk '{total += $1; print} END {print total, "Total"}'

You can do this with a single awk solution, removing the need to any piping to sort or uniq. Using GNU awk:

awk '$9 ~ /[[:digit:]]{3}/ { err[$9]+=1 } END { PROCINFO["sorted_in"]="@val_num_asc";for (i in err) { tot=tot+err[i];print i" - "err[i]} printf "\nTOT - %s\n",tot }' access.log

First we check that the 9th space delimited field is in the correct format for an HTTP code (3 digits). If this is the case, we set up an array called err with the code as the index which is incremented every time the code is encountered. Once all lines are processed, we sort the array in value, number, ascending order and we loop through the entries printing the code and the total. We also set a running total in the variable tot. We finally print this tot at the end of the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM