Home > Back-end >  Awk regix print params from URL request in access log
Awk regix print params from URL request in access log

Time:07-26

I have an access log file containing the following data, I want to check how many times the &u={yyy} parameter appears and print the corresponding number.

192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=123&b=check
192.168.1.2 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check

Resuls: 
2 122
1 123

CodePudding user response:

I would harness GNU AWK for this task following way, let file.txt content be

192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=123&b=check
192.168.1.2 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check
192.168.1.1 [2022/07/10 20:00:00] GET /action?t=test&u=122&b=check

then

awk 'match($0,"&u=[^&]*"){arr[substr($0, RSTART 3, RLENGTH-3)]  }END{for(i in arr){print arr[i],i}}' file.txt

gives output

2 122
1 123

Explanation: I use 2 string functions, first is match which does set RSTART, RLENGTH and its' return value is used as condition, so action is executed only if match was found. Action is simple increase of value of array under key based on match without 3 first characters (&u=). After all lines are processed I output value key pairs of arrays. Disclaimer: this solution assumes any order of output lines is acceptable.

(tested in gawk 4.2.1)

CodePudding user response:

If the logfile always look the same:

cat logfile | awk -F\& '{print $2}'| uniq -c
  • Related