Find the first occurrence of an error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the first occurrence of an error
# 1  
Old 10-17-2011
Find the first occurrence of an error

I have a log file :
Code:
country_error : xxxxxxxxxxxxxxxxxxxxxxxxxx,99999999999999999
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

I have to make a list of different errors , the total number & the line number of the first occurrence of each error...I could list & display total number of each kind , need help to display the first occurrence of each error..
Code:
"cat log_sam.txt | grep "error" | sed 's/^\(.*_error\):\(.*\)/\1/g' | sort | uniq -c| awk -F " " '{print $2,"-",$1}'"==>

country_error - 5
customer_error - 4

Thanks for your suggestions ....

Last edited by Franklin52; 10-17-2011 at 03:44 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-17-2011
Hi Anu_1,

First digit is first occurrence, second digit is total number of appearances.
Code:
$ cat infile 
country_error : xxxxxxxxxxxxxxxxxxxxxxxxxx,99999999999999999
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
customer_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
country_error:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 input-file\n] unless @ARGV == 1;

my ( %error );

while ( <> ) {
        my @f = split /\s*:\s*/;
        next if @f > 2;
        $error{ $f[0] }->[0] = $error{ $f[0] } ? $error{ $f[0] }->[0] : $.;
        $error{ $f[0] }->[1]++;
}

for ( keys %error ) {
        printf qq[%s - %d - %d\n],
                $_,
                $error{ $_ }->[0],
                $error{ $_ }->[1];
}
$ perl script.pl infile 
customer_error - 3 - 4
country_error - 1 - 5

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 10-17-2011
Code:
 perl -e 'while(<>){
  ($error)=split(/\s|:/,$_); # capture the error type
  $errors{$error}{"first"}=$. if ! $errors{$error}{"count"}++;# increment the count for this error and note the line number if this is the first instance.
}
for $error (keys %errors){
  print "There were ", $errors{$error}{"count"}," errors of type $error, the first occurring on line ", $errors{$error}{"first"},"\n";
}'  tmp.data
There were 4 errors of type customer_error, the first occurring on line 3
There were 5 errors of type country_error, the first occurring on line 1


Last edited by Skrynesaver; 10-17-2011 at 05:41 AM..
This User Gave Thanks to Skrynesaver For This Post:
# 4  
Old 10-17-2011
Thanks for your help ...can it be done through shell scripting ....?
# 5  
Old 10-17-2011
Code:
for i in customer_error country_error
do 
    echo "$i - `grep -n $i infile | head -1 | cut -d':' -f1` - `grep -c $i infile`"
done

# 6  
Old 10-17-2011
Quote:
Originally Posted by jayan_jay
Code:
for i in customer_error country_error
do 
    echo "$i - `grep -n $i infile | head -1 | cut -d':' -f1` - `grep -c $i infile`"
done


Thanks ...the number of errors could be more ..in this example only two types are shown...
# 7  
Old 10-17-2011
modified for your requirement ..
Code:
for i in `nawk -F: '/error/ {print $1|"sort"}' infile | sed 's, $,,g' | uniq`
do
    echo "$i - `grep -wn $i infile | head -1 | cut -d':' -f1` - `grep -wc $i infile`"
done

This User Gave Thanks to jayan_jay 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

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

3. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

4. Shell Programming and Scripting

awk to find the number of occurrence

My file contains like this on 10 th line NM1*IL*1* awk '/NM1/{print NR}' *.dat output is 10 awk '/NM1*IL*1*/{print NR}' *.dat output is Nothing but im expecting 10 on second code as well . (4 Replies)
Discussion started by: Rajesh_us
4 Replies

5. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

6. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

7. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

8. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

9. UNIX for Dummies Questions & Answers

optimizing - to find the number of occurrence

Hi, I need to find the number of occurrence of string in a file, for ex: >cat filename abc abc def ghi ghi ghi ghi abc abc >output would be abc 4 def 1 (10 Replies)
Discussion started by: matrixmadhan
10 Replies

10. Shell Programming and Scripting

stop unix find on a directory structure after finding 1st occurrence

Hi, Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and... (3 Replies)
Discussion started by: jm0221
3 Replies
Login or Register to Ask a Question