Count percentage of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count percentage of string
# 1  
Old 09-03-2014
Count percentage of string

Hi,

Am trying to count the number of occurrences and then as a percentage from a log to troubleshoot errors, can someone help please

Code:
grep -o 'HTTP/1\.1\" 404 3..' access_log | wc -l
6


Last edited by Don Cragun; 09-04-2014 at 06:43 AM.. Reason: Add CODE tags.
# 2  
Old 09-03-2014
This sounds like a homework assignment.
# 3  
Old 09-04-2014
So... Is this a homework assignment?
# 4  
Old 09-04-2014
Of course not!

Im trying to develop an app to read my logs for errors
# 5  
Old 09-04-2014
Code:
awk '/HTTP\/1\.1\" 404 3../ { E++ } END { printf("%d/%d=%d%%\n", E, NR, (E/NR)*100); }' logfile

# 6  
Old 09-04-2014
You must forgive me, but this would make a nice homework question.

It's concise, has escaping RE characters, floating point calculations/rounding and formatting.

Particularly if awk was forbidden. Here is show the percentage to 2 decimal places of precision:

Code:
NUM=$(grep -c  'HTTP/1\.1" 404 3..' access_log)
RECS=$(wc -l < access_log)
printf "Found %d records from %d (%.2f %%)\n" $NUM $RECS $(echo "scale=3 ; $NUM * 100 / $RECS " | bc)


Last edited by Chubler_XL; 09-04-2014 at 05:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

2. Shell Programming and Scripting

how to count string length?

Hi, Gurus, I have a requirement which need count string length. eg. abc bcde defge want to get following: abc 3 bcde 4 defge 5 :wall: thanks in advance! (6 Replies)
Discussion started by: ken002
6 Replies

3. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

4. Shell Programming and Scripting

To count a string with in a variable

I am writing a ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or... (6 Replies)
Discussion started by: vpv0002
6 Replies

5. Shell Programming and Scripting

count of a string within a variable

I am writing ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or... (3 Replies)
Discussion started by: vpv0002
3 Replies

6. Shell Programming and Scripting

awk script to count percentage from log file

Hi, I have a log like this : actually i want to get the log like this : where % can get from : 100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq Thanks in advance.. :) (8 Replies)
Discussion started by: justbow
8 Replies

7. Shell Programming and Scripting

count times for one string

I have a file. I want to count the time for one string appears in this file Example: 56 73 34 79 90 56 34 Expected results 2:56 1:73 2:34 (1 Reply)
Discussion started by: anhtt
1 Replies

8. Shell Programming and Scripting

Count string at header.

Hi there, it may quite easy but i've tried my best to editing this awk script but didn't get to solution. Scripts:BEGIN { Header = "1 "; FileType = "NBI"; #Count = Count + 1; printf("%-2s%-3s%03s%-14s%-84s\r\n", Header, FileType, Count, FileSeq, SysTime, " "); } { ... (13 Replies)
Discussion started by: Helmi
13 Replies

9. UNIX for Dummies Questions & Answers

How to count the string length

Please can anyone tell me, how to count the string length (2 Replies)
Discussion started by: Anshu
2 Replies

10. Shell Programming and Scripting

count from right end of string

hi all, I'm trying to use substr(p,c,l) in awk but i'd like to have the 'position' argument count from the right end of the string instead of left end. evidently, just putting a '-' sign in front of the number does not work. any ideas? cheers, kl (1 Reply)
Discussion started by: ee7klt
1 Replies
Login or Register to Ask a Question