LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2011, 05:20 AM   #1
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Rep: Reputation: 0
match multiple lines in a text file - BASH


Hello,

in my bash script, I generate a text file in a loop and for each step in the loop some text is appended to the file but at some points (where the loop counter is two in the below example) I got an error message which is also directed to the text file ( but the scripts continues running). I want to construct a conditional statement like:
if (there is an error message); then
do something
fi.

my specific problem lies on the condition part. I thought about trying to find the pattern in the textfile :

Quote:
*****************
2 (loop counter - $i)
*****************

ERROR: no file found
I could then use this (if there is such a pattern or not) to solve my problem. But I could not find how to match multiple lines in bash script. Finding only one line works well with sed or grep but could not manage that for multiple lines.
Any other idea other than my approach is also very welcome.
I would really appreciate some help.
thanks!
Code:
command >> textfile 2>$1
Code:
sometexthere
*****************
1 (loop counter)
*****************
sometexthere
sometexthere
*****************
2 (loop counter)
*****************

ERROR: no file found
sometexthere
sometexthere
 
Old 12-21-2011, 05:37 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Why not just test if file exists before proceed ?
 
Old 12-21-2011, 05:53 AM   #3
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

if [[ file -e ]]; then continue else do something fi
 
Old 12-21-2011, 07:30 AM   #4
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Original Poster
Rep: Reputation: 0
the text file exists, no need to check for that. maybe I explained a bit complicated.
in short:
I want to find the match of multiple lines in the text file to see at which step I have an error message.
for that I need to find the loop index number (2 in below case) and the error message together as a block. for one line I can use sed or grep, for example:

sed -n /"ERROR: no file found/" textfile

but that is not enough, because then I do not see the loop index number, that is why i want to grep the multiple lines at a time. I already spend sometime but could not find the right syntax for that.

*****************
2
*****************

ERROR: no file found
 
Old 12-21-2011, 08:00 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I think you need to back up a bit and show us the file being parsed and your script. This will help
us inform you where the error is instead of you guessing and trying to tell us how to help you.
 
Old 12-21-2011, 08:31 AM   #6
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Original Poster
Rep: Reputation: 0
The error itself is not the problem, the problem is to find out where I have the error..
hope this helps:


code:

Quote:
for ((i=1; i<=4; i++)); do

#write stdoutput and the stderr to a textfile
command to excute a function of a software >> textfile 2>&1

error=${sed -n /"ERROR: no file found/" textfile} #here is the problem. I want to check here if there is an error message on my textfile for ith step

if (there is an error message for ith step);then #I also have to construct the condition
do something #this part is no problem
fi

done
textfile generated by above command:

Quote:
sometexthere
*****************
1
*****************
sometexthere
sometexthere
*****************
2
*****************

ERROR: no file found
sometexthere
sometexthere
*****************
3
*****************

ERROR: no file found
sometexthere
sometexthere
*****************
4
*****************
sometexthere
sometexthere
 
Old 12-22-2011, 06:02 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
You want to scan for “ERROR: no file found” and then for the third line above? I sometimes process such files backwards:
Code:
$ tac filename | sed -n "/ERROR: no file found/{n;n;n;p}"
3
2
You could add tac another time to get the final list in forward order again.

Last edited by Reuti; 12-22-2011 at 06:03 AM.
 
Old 12-22-2011, 10:35 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

Do NOT use "quote" tags, which don't preserve whitespace.

Code:
command >> textfile 2>&1
Redirections are evaluated left to right. This first redirects stdout to the text file, then it redirects stderr to the same place as stdout -- i.e. the text file.

Perhaps you can use this instead (assuming the error messages are being printed to stderr):

Code:
command >> textfile 2>/dev/null
Now stderr is simply disposed of, rather than redirected to the file.

Or this:
Code:
command 2>&1 >>textfile
Which should redirect the stderr output to the screen (the default target of &1), then redirect the stdout to the text file. The file then shouldn't get the error messages, but they'll be visible to you in the terminal.

Or just leave the stderr redirect off completely; since it defaults to the screen anyway.

http://wiki.bash-hackers.org/syntax/redirection

Last edited by David the H.; 12-22-2011 at 10:54 AM. Reason: small addition
 
Old 12-22-2011, 11:22 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Hmm. I just read the thread a bit more carefully. This is more for debugging the external command than the existence of the errors themselves?

Code:
grep -B3 'ERROR' textfile
You should then be able to easily parse the output for the numbers you want.

Assuming you use bash or another shell with arrays:

Code:
errors=( $( grep -B3 'ERROR' textfile | egrep '^[0-9]+$' ) )

for i in "${errors[@]}"; do
	...
done
Perhaps not the cleanest solution, but it does the job.
 
  


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
[BASH] How to read multiple lines from a text file? gtirsmiley Programming 13 03-11-2011 10:18 AM
[BASH] How to read multiple lines from a text file? gtirsmiley Programming 1 03-11-2011 06:35 AM
match text over multiple lines Python donnied Programming 5 05-24-2009 09:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 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
Open Source Consulting | Domain Registration