Skip to content
Advertisement

How to group by two fields using bash scripting?

Here is one example of one line of log:

2016-04-24 23:59:45 -1  6bd3fbb8-65ac-4d16-bf32-48659a76c499    2           +15173583107    14      +161760555935   14      de.xxxx-O2  layxxxd 0   1

I know how to group by one filed, so this is the solution:

awk '{arr[$11]+=$12} END {for (i in arr) {print i,arr[i]}}' exmaple.log

and this would be results:

xx 144
layxxxd 49.267

My question is that how can I group by two fields instead of one, first should be $11 and second is $10? So results should change to:

layxxxd unknown 100
layxxxd de.xxxx-O2 44

Advertisement

Answer

how can I group by two fields instead of one, first should be $11 and second is $10?

You can use $11 FS $10 as your key for associative array:

awk '{arr[$11 FS $10] += $12} END {for (i in arr) {print i,arr[i]}}' exmaple.log
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement