Help with script to read lines from file and count values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script to read lines from file and count values
# 1  
Old 04-27-2011
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:

Code:
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

Last edited by joeyg; 04-27-2011 at 04:21 PM..
# 2  
Old 04-27-2011
Code:
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:
Code:
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

This User Gave Thanks to mirni For This Post:
# 3  
Old 04-27-2011
Code:
$ awk -F \/ '{a[$4]++}END{for (i in a) print i " : " a[i]}' /opt/Apache/logs/access_log.11-04-2011

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

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

2. Shell Programming and Scripting

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. UNIX for Dummies Questions & Answers

Count the lines with the same values in a column and write the output to a file

Hey everyone! I have a tab delimited data set which I want to create an output contained the calculation of number of those lines with a certain value in 2nd and 3rd column. my input file is like this: ID1 1 10M AAATTTCCGG ID2 5 4M ACGT ID3 5 8M ACCTTGGA ID4 5 ... (7 Replies)
Discussion started by: @man
7 Replies

5. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

6. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

7. Shell Programming and Scripting

Why does my script only read two lines of a file and not the third

I'm learning about the read command and wrote this little script to read data from a file: readfile() { while read variable; do echo $variable done } readfile < File.txt I have three lines in File.txt; each a single word. The script only echoes the first two lines and drops the... (9 Replies)
Discussion started by: Straitsfan
9 Replies

8. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

9. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies

10. Shell Programming and Scripting

Korn Shell Script - Read File & Search On Values

I am attempting to itterate through a file that has multiple lines and for each one read the entire line and use the value then to search in other files. The problem is that instead of an entire line I am getting each word in the file set as the value I am searching for. For example in File 1... (2 Replies)
Discussion started by: run_unx_novice
2 Replies
Login or Register to Ask a Question