LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-18-2012, 03:05 PM   #1
jgm27
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Rep: Reputation: Disabled
Parse log file for errors and fetch variable number of lines before and after error


I have a log file containing data and error information from multiple sources whose only error report similarity is the word ERROR. When I hit this word I need to check the info after this on the same line against an error list and then retrieve lines prior to and after this line depending on the error matched.
e.g. 404 2 1
500 3 0
for error 404 get 2 before and 1 after and 500 3 before and none after.
Many of these errors are strings e.g. 'Bad Gateway'
I can do this easily using grep but to achieve this I am parsing the file once for each error type.
For some I have to read back till another string is hit and this I can't do using grep.
Has anyone an idea of how I can achieve all of this using only one pass of the log file.
I presume awk is the answer but don't know where to start.
Any help would be much appreciated.
Thank you.
 
Old 07-18-2012, 04:17 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
You could use Perl and the built-in line counter var '$.' http://perldoc.perl.org/perlvar.html and seek fn http://perldoc.perl.org/functions/seek.html
 
Old 07-18-2012, 04:50 PM   #3
jgm27
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks Chris. Don't currently know perl too well but I guess this is a good place to start to learn.
 
Old 07-18-2012, 05:00 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
No worries; its great at this sort of thing. That is the main Perl docs site; everything you need, explained with examples for each cmd.
See also http://www.tizag.com/perlT/index.php, http://www.perlmonks.org/?node=Tutorials.
I also highly recommend The Perl Cookbook; worth its weight in $mineral-of-your-choice

Last edited by chrism01; 07-18-2012 at 05:01 PM.
 
Old 07-18-2012, 05:26 PM   #5
jgm27
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you again Chris.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] repeating lines X number of times based on variable using awk(?) captainentropy Linux - Newbie 2 06-25-2012 02:19 PM
Parse log file for some error lovelysandu Programming 10 05-26-2009 05:56 AM
Fetch x lines from file G00fy Programming 17 07-18-2008 01:13 PM
Parse lines need from /var/log/message but excluding... grant-skywalker Linux - General 8 03-20-2007 02:30 PM
Getting Line Number in Error Log File kushalkoolwal Programming 6 10-17-2005 12:50 PM


All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration