Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log.

An example of the log file content:

123.23.111.23 - - [10/Apr/2011:23:59:03 +0100] "POST /GetID/Star HTTP/1.0" 503 693 60014262
333.222.111.43 - - [11/Apr/2011:00:00:02 +0100] "POST /SearchAccount_V2/Star HTTP/1.1" 200 1155 462951
111.888.222.44 - - [11/Apr/2011:00:00:02 +0100] "POST /SearchModem_V2/Star HTTP/1.1" 200 1161 993251
777.22.555.33 - - [11/Apr/2011:00:00:04 +0100] "POST /BillingCode/Star HTTP/1.1" 200 778 90597

So on the 1st line I wish to extract GetID, then the script would count how many instances of GetID there are in the log file.

ordinarily I would use the following if I was just wanting to search for a single entry:

grep GetID /opt/Apache/logs/access_log.11-04-2011 | grep wsdl | wc -l

I have a separate file listing all of the webservices (118 in total) that could be an input into the script.

the output file would look something like:

GetID : 345
SearchAccount_V2 : 687
etc etc

many thanks

grep -c  GetID  /opt/Apache/logs/access_log.11-04-2011

Will count lines containing GetID

Stick it into a loop to count the different patterns:

log=/opt/Apache/logs/access_log.11-04-2011
while read service ; do 
    echo "Service $service occurrs $(grep -c $service $log) times"
done < $service_list
1 Like
$ awk -F \/ '{a[$4]++}END{for (i in a) print i " : " a}' /opt/Apache/logs/access_log.11-04-2011

SearchModem_V2 : 1
GetID : 1
SearchAccount_V2 : 1
BillingCode : 1

1 Like