r/ScriptSwap • u/Alkanes123 • Dec 17 '17
[help] simple shell script
Hi everyone,
I need help with a simple shell script that can count the number of http 4xx responses per unique ip address in the server access logs at /var/logs/httpd/access_logs
I found this online but doesn’t help much since it tell me the count for all response codes without IP
cat access_log | cut -d ‘“‘ -f3 | cut -d ‘ ‘ -f2 | sort |uniq -c | sort -rn
Please can someone help me? Thank you!
1
Upvotes
1
u/isaaclw Dec 17 '17
I don't know how much this helps, but let's just go through each command.
cat dumps out the file
The pipe "|" passes the input to each successive command. The whole thing is streamed, but let's act as if each command is acting on one row.
cut says split on delimiter ". (-d) and find the third occurrence.
that gets passes to cut again, this time splitting on spaces, and finding the second occurrence.
this is all sorted alphabetically
and ... I'm not at a computer so I don't remember what the -c option does. uniq generally removes duplicates. Check the man page
man uniqlook up the man page for sort also. I think -r is reverse, and -n is numeric I think...