Unix/Linux Go Back    


BSD BSD, sometimes called Berkeley Unix, is a Unix operating system developed by the Computer Systems Research Group of the UC Berkeley.

Searching in multiple files

BSD


Tags
find, grep, script, search

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-01-2014
jim john jim john is offline
Registered User
 
Join Date: Sep 2014
Last Activity: 25 February 2015, 8:08 AM EST
Location: Cochin ,India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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 09:06 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 09-01-2014
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 28 April 2017, 1:46 PM EDT
Location: Chennai
Posts: 2,430
Thanks: 544
Thanked 1,142 Times in 1,028 Posts
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
Sponsored Links
    #3  
Old Unix and Linux 09-01-2014
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 April 2017, 2:28 AM EDT
Location: Aachen, Germany
Posts: 10,703
Thanks: 263
Thanked 3,284 Times in 3,025 Posts
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 Unix and Linux 09-01-2014
jim john jim john is offline
Registered User
 
Join Date: Sep 2014
Last Activity: 25 February 2015, 8:08 AM EST
Location: Cochin ,India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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 09:02 AM.. Reason: Please use code tags
Sponsored Links
    #5  
Old Unix and Linux 09-04-2014
MadeInGermany MadeInGermany is online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 28 April 2017, 1:36 PM EDT
Location: Simplicity
Posts: 3,508
Thanks: 273
Thanked 1,164 Times in 1,056 Posts
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

The Following User Says Thank You to MadeInGermany For This Useful Post:
rbatte1 (09-08-2014)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help on searching for a string on multiple files newbie_01 Shell Programming and Scripting 2 04-17-2014 06:14 AM
Searching across multiple files if pattern is available in all files searched novice_man Shell Programming and Scripting 6 02-13-2011 09:02 PM
Searching a word in multiple files rakesh_411 Shell Programming and Scripting 3 10-10-2010 04:28 AM
Searching for multiple patterns in files bornon2303 Shell Programming and Scripting 5 02-25-2010 09:05 AM
Searching multiple files with multiple expressions Anahka Shell Programming and Scripting 6 01-07-2004 05:24 PM



All times are GMT -4. The time now is 01:53 PM.