Using awk to extract IPs with exact match on string?

Is there a way to use a exact match on the condition in awk?

This is what I have:

Code:
awk '$11 == "200" && index($9, "/example") {print $1}' /var/log/httpd-access.log | sort -n >> /tmp/ips.list

This catches '/anotherdirectory/example' too as it contains '/example'.

Is there a way to prevent this?

Thanks
 
awk '$11 == "200" && $9 == "/example" {print $1}' /var/log/httpd-access.log

or

try with ^ (start of the line) $ (end of the line)

^\/example$

awk '$11 == "200" && $9~/^\/example$/ {print $1}' /var/log/httpd-access.log

$11 - column 11
== string comparison equal
"200"

&& logical and

$9 - column 9
~ regex match
/ start of the regex
^ start of the line
\/ escape "/" character
example search string
& end of the line
/ end of the regex


You can test it here with:

/dir1/example
/example


Btw my column numbers are different in httpd-access.log
awk '{for (i=1;i<=NF;i++) print "Column "i" : " $i ""} NR==3{exit}' /var/log/httpd-access.log
 
Back
Top