How to get filename from the fullpath and how to grep multiple strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get filename from the fullpath and how to grep multiple strings
# 1  
Old 10-03-2010
How to get filename from the fullpath and how to grep multiple strings

Hi,

New to shell scripting....
I have log file content as below:

Quote:
/logs/S1/logfilename1.log.gz:01/20/2010 14:43:57 ERROR [128500] Exception is caught.
/logs/T15/logfilename2.log.gz:01/20/2010 14:43:57 ERROR [83D9H7] Exception is caught.
I have to count the number of occurences of ERROR or INFO Messages.
So, I cut 5 th column and uniquly sorted and redirected it to new.txt file.

But I want copy to S*/Filename and T*/Filename of respective ERROR or INFO messages,uniquly sorted to new.txt file.

I tried awk command ..but it failed to copy to new.txt file.

Once this is done I have to check for the occurences of ERROR string. So for that I searched for egrep and tried to use, but output is incorrect.
Final output i am looking for:

Quote:
S*/Filename,Exception is caught., 20
T*/Filename,Exception is caught. ,30
Do anybody have any idea???

Thanks in advance..

Last edited by Shirisha; 10-06-2010 at 03:17 PM..
# 2  
Old 10-03-2010
Try:
Code:
perl -ne '/(?<=logs\/)([^\/0-9]+).*?\/([^:]+).*(?<=] )(.*?)$/;$a{"$1*/$2,$3"}++;END{for $i (keys %a){print "$i, $a{$i}\n"}}' logfile


Last edited by bartus11; 10-03-2010 at 04:50 PM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-03-2010
Another approach:
Code:
awk '/ERROR|INFO/{sub(".*logs/","");sub(":.*","");a[$0]++}
END{for (i in a)print i, "Exception is caught", a[i]}' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 10-03-2010
Code:
awk -F':|\] ' '/ERROR|INFO/{sub(".*logs/","",$1);a[$1 OFS $NF]++}END{for(i in a)print i,a[i]}' file

This User Gave Thanks to danmero For This Post:
# 5  
Old 10-03-2010
Code:
 $ ruby -ne 'BEGIN{h={};h.default=0};(a=$_.scan(/^.*logs\/(.[^\/]*)(.[^:]*)/).join; h[a]+=1) \ 
   if /ERROR|INFO/;END{h.each{|x,y| print "#{x} Exception caught #{y}\n" } } ' file


Last edited by kurumi; 10-03-2010 at 09:27 PM..
This User Gave Thanks to kurumi For This Post:
# 6  
Old 10-03-2010
Thanks All for reply...

Last edited by Shirisha; 10-06-2010 at 03:16 PM.. Reason: updated problem description
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep multiple strings in a file

Consider i have the below data in my log file. i want to grep using "Monday" and "Working" So the only output i expect is Can you help me with the grep query for Sun Sparc ? Usage: grep -hblcnsviw pattern file . . . (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Whether we can search multiple strings using or in grep -F

Hi, Whether we can search multiple strings using or in grep -F In Generally, grep -F "string1" "filename.txt" How to search for multiple string using grep -F as we using grep grep "string1\|string2" "filename.txt" Regards, Nanthagopal A (10 Replies)
Discussion started by: nanthagopal
10 Replies

3. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

4. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

8. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

9. Shell Programming and Scripting

Efficient way to grep multiple strings

I have a script which searches a huge log file for the existence of a specified string and if the string is not present i receive an alert mail. Here's an extract: STRING=$(grep 'warning' logfile | tail -1 | wc -l) if (( ${STRING} > 0 )); then print -- "---- Warning etc.... (3 Replies)
Discussion started by: Moxy
3 Replies

10. UNIX for Dummies Questions & Answers

Extracting Filename from Fullpath

Hi, Any help on this would be very appreciated. I capture the full path & filename in a variable like (varFile=/home/user/extfile.txt). Now in my shell script I have to use only the filename part i.e. extfile.txt. How do I extract only the filename part from the variable? Thanks in... (3 Replies)
Discussion started by: njoshi
3 Replies
Login or Register to Ask a Question