Searching in multiple files


 
Thread Tools Search this Thread
Operating Systems BSD Searching in multiple files
# 1  
Old 09-01-2014
Searching in multiple files

I am new to unix and I would like to search multiple log files to find earliest occurrence of a text.
Ex:
Say I have 10 logs file each ending with .log and I want to find the text “CustomeError” .
I want to find the which log file “CustomeError” comes first and lines which surround’s “CustomeError”.
Also I would like to find the last occurrence of “CustomError”
Every line in my log file starts with date and time.
Ex :
Code:
8/31/14 7:53:05:954 
9/1/14 0:21:08:050

I am OK with script or single command.

Last edited by Franklin52; 09-01-2014 at 10:06 AM.. Reason: Please use code tags
# 2  
Old 09-01-2014
Hello Jim,

Welcome to the forum, as per forum rules could you please use code tags for commands and codes before posting you can also use preview post option for same.
Also kindly do let us know the input flle and expected output for same with OS details, it will help us to guide you.


Thanks,
R. Singh
# 3  
Old 09-01-2014
Sounds like an application for grep -C, if that is available on your system (it is on FreeBSD).
If you really need to search for “CustomeError”, “CustomError”, or even “CustomerError”, put a wildcard into the search pattern.
# 4  
Old 09-01-2014
Example input file:
I will have more files like this.
See in this file i want to find the line
Code:
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.abc.MyClass CustomeError"

which is the earliest occurrence of custome error.
I want my search to extend to all the log files in the folder and find first "CustomeError" in log file.
Code:
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.abc.MyClass CustomeError"
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.abc.MyClass CustomeError"
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[9/1/14 0:21:08:050 EDT] 00000030 SystemErr     R     at com.abc.MyClass CustomeError"


Last edited by Franklin52; 09-01-2014 at 10:02 AM.. Reason: Please use code tags
# 5  
Old 09-04-2014
Perhaps your grep can stop after the 1st match
Code:
grep -m 1 "CustomeError" *.log

Also display the surrounding lines ("Context"):
Code:
grep -m 1 -C 1 "CustomeError" *.log

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

Help on searching for a string on multiple files

Hi all, I am sure some gurus will find a better way of doing this. FYI, I've manually modified some of the data 'coz they are somewhat confidential, so there may be some typo errors. At the moment, I have 3 files that I am trying to search for. Sometime in the future, it may go beyond 3... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Searching for similar row(s) across multiple files

Hello Esteemed Members, I need to write a script to search for files that have one or more than one rows similar. Please note that there is no specific pattern that I am searching for. The rows can be different, I just need to find out two or more similar records in two or more files. There... (7 Replies)
Discussion started by: Yoodit
7 Replies

3. UNIX for Dummies Questions & Answers

Grep in Perl - Searching through multiple files

I'm attempting to use grep in Perl with very little success. What I would like to do in Perl is get the output of the following grep code: grep -l 'pattern' * This gives me a list of all the files in a directory that contain the pattern that was searched. My attempts to do this in Perl... (4 Replies)
Discussion started by: WongSifu
4 Replies

4. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

5. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

6. Shell Programming and Scripting

Searching a word in multiple files

Hi All, I have a issue in pulling some heavy records , I have my input file has 10,000 records which i need to compare with daily appended log files from (sep 1st 2009 to till date) . I tried to use grep fgrep and even sed , but the as time is factor for me , i cannot wait for 5 days to get the... (3 Replies)
Discussion started by: rakesh_411
3 Replies

7. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

8. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

9. Shell Programming and Scripting

Searching for multiple criteria in log files?

I would like a simple shell script that will allow me to display to screen all unsuccessful su attempts in my sulog file, for the present date. I have been trying several different combinations of commands, but I can't quite get the syntax correct. The mess I have right now (don't laugh) is... (4 Replies)
Discussion started by: Relykk
4 Replies

10. Shell Programming and Scripting

Searching multiple files with multiple expressions

I am using a DEC ALPHA running Digital UNIX (formly DEC OSF/1) and ksh. I have a directory with hundreds of files that only share the extension .rpt. I would like to search that directory based on serial number and operation number and only files that meet both requirements to be printed out. I... (6 Replies)
Discussion started by: Anahka
6 Replies
Login or Register to Ask a Question