LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-18-2009, 10:33 AM   #1
captain_cthulhu
LQ Newbie
 
Registered: May 2009
Posts: 5

Rep: Reputation: 0
Question possible with grep? find string and then output that and the rest of the file


just wondering if this is possible using grep in conjunction with other commands - I am not really new to using Linux or the grep command but I am pretty new to many commands like sed and awk, so yeah, I am new to linux

I have a log file (text format) that puts date stamps on the first line of an error. the full error output however, could be made up of many lines (variable) that do not contain the date stamp. when I do a straight grep command, I only get the line containing the date I am searching for but I lose the error data contained on the lines below that.

I know that gnu grep can output a set number of lines after the found string but I don't have that installed and I am not able to add it to these machines (I can't add non-essential stuff to these production boxes). besides, since the error can be a variable number of lines, I can't use a set number to catch everything.

since these log files are very large, I'm just trying to cut my losses by finding my matching string with grep and then just output the whole rest of the file - this will at least cut down on the size of the output and can allow for manual perusing from the top of the output.

I'm new to these forums (just singed up a few mins ago! w00t!) and I thought this was the best place for me to ask this, sorry if it's not. any help is appreciated. thx!
 
Old 05-18-2009, 10:37 AM   #2
captain_cthulhu
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
sorry, I found this after I posted:
http://www.linuxquestions.org/questi...a-file-450825/

I'm going to play around with this but if anyone has any suggestions, they are welcome.

one small difference is that I'm looking for string date like 'May 14' rather than a formal date format.
 
Old 05-18-2009, 10:46 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using awk you can do something like
Code:
awk '/^May 14/{out="yes"}{if ( out ) print}' /path/to/log/file
Take in mind that in awk a condition is considered false if its value is zero or the null string; otherwise, the condition is true. In this case the variable out is a null string until "May 14" is encountered at the beginning of a line.
 
Old 05-18-2009, 11:28 AM   #4
captain_cthulhu
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
perfect! thanks a ton and this will help me learn a bit more about awk
 
Old 05-18-2009, 12:09 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Or, slightly more lazy, use a range - with an end pattern
that doesn't exist.
Code:
awk '/^May 18/,/^@@@/' messages


Cheers,
Tink
 
  


Reply



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
grep for string in file after a certain part of the file B-Boy Programming 6 02-18-2009 07:49 AM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 06:29 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 09:12 AM
Grep string and redirect the rest to a file twantrd Programming 6 06-02-2006 07:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:43 PM.

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
Open Source Consulting | Domain Registration