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
 
Great!! Thanks for your help and tips, much appreciated!! 🙏

I guess I actually need it to match from the start of the line, so I removed the $ character.
I'll hope that will do the trick:

Code:
awk '$11 == "404" && $9~/^\/example/ {print $1}' /var/log/httpd-access.log | sort
 
OK I see, thanks for pointing that out! But it wont checking for a match inside long directory structures, like the old one i had?

/tools/hammers/example/thor
 
It will skip strings that do not start with /example, so it won't match /tools/hammers/example/thor, but it will match /examplesomething_else.

If you want to match exactly /example, you could regex ^\/example$

^ matches the beginning of the string, $ matches the end of a string. If you want to match with a / at the end, that may or may not be there, you could use ^\/example(\/)?$

 
Back
Top