LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-01-2010, 11:31 PM   #1
saheervc
LQ Newbie
 
Registered: Mar 2009
Posts: 3
Blog Entries: 1

Rep: Reputation: 0
multiple pattern search in a single file line by line


Hi Guys, Im a new to Linux.
Can some one help me to sort out the following problem.

I have a very long output file ( some 104735 lines ) generated by a program. I have to check this output file for errors. The file looks like the following .

...............................................................
I=1
...
...
NO CONVERGENCE
....

..
I=2
...
..
CONVERGENCE REACHED
...
...
..
I=3
...
..
NO CONVERGENCE
...
..
MAXIMUM NUMBER OF ITERATIONS REACHED
...
..
etc upto I=48

..................................................................
It is very difficult to go through each line for errors.

I would like to write a script file that prints only errors under iteration number in the following way.
.........................................................
I=1
NO CONVERGENCE
I=3
NO CONVERGENCE
MAXIMUM NUMBER OF ITERATIONS REACHED
...

.........................................................

Im using Mandriva 2010
Thanks in Advance.
 
Old 09-01-2010, 11:44 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/^I=/{i=$0}/^NO CONVERGENCE|MAX.*ITERATIONS.*REACHED/{print i,$0}' file
 
1 members found this post helpful.
Old 09-01-2010, 11:45 PM   #3
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
Hi, welcome to LQ!

Given the data above something like
Code:
egrep "^I=|NO CONVERGENCE|MAXIMUM NUMBER OF ITERATIONS REACHED" file
should do. You'd be getting the I=2, too, because there's no
multi-line match with a back-reference here.


If the file structure were clearer we could probably use an
awk script to eliminate the the "successful" iterations number
from the output.


Cheers,
Tink
 
1 members found this post helpful.
  


Reply

Tags
pattern, print, 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
Search values within multiple files line to line Chrizzieej Programming 5 09-26-2008 04:11 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

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

All times are GMT -5. The time now is 12:39 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