Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-18-2009, 10:33 AM   #1
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
LQ Newbie
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
sorry, I found this after I posted:

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
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Using awk you can do something like
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
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
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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



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
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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:03 PM.

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