egrep parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep parameters
# 1  
Old 10-30-2008
Java egrep parameters

Hi All,

i have to check a log file for the patterns like error, msg, fail or warn...
for this i can do, like

here log file is the input file to the script

egrep -i "error | fail | warn | unable" logfile.log
if [ $? -eq 0 ]
then
echo " patteren found "
else
echo " pattern not found "

but, what i want here is i want to know which are all the patterns found and which are all the patterns not found.

so that i can print the patterns which are all found an not found seperately for a precise manner.

my expected output is:

error, warn found in the log file
fail, unable not found in the log file
# 2  
Old 10-30-2008
Hammer & Screwdriver

Do each of your four greps separately.
Use similar logic on return values, just instead of saying fount/not, echo what you just searched for to a variable/file for found/not.
After doing all four, that variable/file will hold the paramaters that met the condition.
# 3  
Old 10-31-2008
Java

Thanks Joeyg,
i want to accomplish with less LOC.

could you please guide me, to make this effective.
Thanks.
# 4  
Old 10-31-2008
A solution with awk:

Code:
#!bin/sh

awk '
{$0=tolower($0)}
/error/{m[1]++}
/fail/{m[2]++}
/warn/{m[3]++}
/unable/{m[4]++}
END{
printf("%s\n",m[1]?"Error found":"Error not found")
printf("%s\n",m[2]?"Fail found":"Fail not found")
printf("%s\n",m[3]?"Warn found":"Warn not found")
printf("%s\n",m[4]?"Unable found":"Unable  not found")
}' logfile

# 5  
Old 11-01-2008
Java

Thanks Franklin,

could you please tell me how my input file is used in this code and why we
are using m[1]++,m2,etc..

i am novice user.
your help helps me a lot.

Thanks.
# 6  
Old 11-01-2008
An explanation of the code:

Code:
{$0=tolower($0)}

To ignore case distinctions, we convert the line to lowercase to match the patterns in lowercase.

Code:
/error/{m[1]++}
/fail/{m[2]++}
/warn/{m[3]++}
/unable/{m[4]++}

For every pattern we use an array m, m[1] for errors, m[2] for fails and so forth.
If a pattern matches we increase the array with 1.

Code:
END{
printf("%s\n",m[1]?"Error found":"Error not found")
printf("%s\n",m[2]?"Fail found":"Fail not found")
printf("%s\n",m[3]?"Warn found":"Warn not found")
printf("%s\n",m[4]?"Unable found":"Unable not found")

After we read all the records we use a printf command with a conditional expression to print the results. If the value of one of the element of the array is true ( > 0 ) the first string ("Error found") is printed otherwise we print the second string ("Error not found").


Regards
# 7  
Old 11-02-2008
function check_log
{
awk '{$0=tolower($0)}
/error/{err++}
/fail/{fail++}
/msg/{msg++}
/unable/{unable++}

/unsuccess/{uns++}
END{ print("%s\n",err?"Error found":"Error not found")
print("%s\n",fail?"Fail found":"Fail not found")
print("%s\n",msg?"Msg found":"Msg not found")
print("%s\n",unable?"Unable found":"Unable not found")
print("%s\n",uns?"Unsuccess found":"Unsuccess not found")
}' logfile

i created like this but i got the error for the lines in bold
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

egrep help

hi, i'm using egrep -i to search thru some text files for keywords (also stored in a file). egrep does wildcard search aka %keyword% as long as the keyword is found, it will be spool to a file meaning if keyword is 'xyz' 123 aabgdggjxyzslgj 124 xyzgjksgjd returns 123... (8 Replies)
Discussion started by: bing
8 Replies

2. Shell Programming and Scripting

egrep

Hi, I am wondering if it's possible to link multiple patterns with egrep. Here here is what I am doing : grep -v ";;" | grep -v ARC_TIME | grep -v \* | grep -v STAS0 Can I do all of this by invoking egrep just once ? Thanks (4 Replies)
Discussion started by: Aswex
4 Replies

3. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

4. UNIX for Dummies Questions & Answers

help on egrep

HI, I have two files filea, fileeb filea z283110z67 xx65686377 xx654681zz xx652836xx xx653881zz xx65480z11 xx654z5466 xx65510000 xx65670000 xx656z0000 xx656z1822 fileb (3 Replies)
Discussion started by: krao
3 Replies

5. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

6. Shell Programming and Scripting

ls and egrep together

Hello, Why is this not returning files containing the string in the var $files? files=`ls /dir1/dir_level2/dir_level3 | egrep "catch \["` files=`ls /dir1/dir_level2/dir_level3` this by itself returns a list of files which I thought could be sent through grep or egrep to look for matches. ... (5 Replies)
Discussion started by: gio001
5 Replies

7. UNIX for Dummies Questions & Answers

egrep

HI, I want to grep the contents of fileb from filea. filbeb 5x4xxx371x31a43 4x40x037103a049 3x4x003710a0659 4x4x0x371a50912 filbea 5x4xxx371x31a43 3266000225 4x4003266000277 3266000277 4x400326x000499 3266000499 4x4003266000676 3266000676 4x4x0x371a50912 3266000777... (4 Replies)
Discussion started by: krao
4 Replies

8. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies

9. Shell Programming and Scripting

egrep help

How can i make something like if (echo "$arg2" | egrep -s '^+\.+km/h+$|^+km/h+$'); then not to output the value of $arg2 on the screen, evertime i get match it outputs the value of the variable on the screen which i don't need to do. I know for grep its -q option but it doesn't work for egrep.... (2 Replies)
Discussion started by: Vozx
2 Replies

10. UNIX for Dummies Questions & Answers

Egrep Help

I'm writing a small script thats purpose is to validate a single command line argument to make sure it is an integer. Also acceptable are a leading "+" or "-", but no more than one. Example: "5" "-2" "+4" are all valid If its invalid I simply print out a message saying so, otherwise I... (2 Replies)
Discussion started by: FuzzyNips
2 Replies
Login or Register to Ask a Question