niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Emulating egrep with sed
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Stephane Chazelas
*nix forums Guru


Joined: 23 Sep 2005
Posts: 490

PostPosted: Mon Jan 09, 2006 7:09 pm    Post subject: Re: Emulating egrep with sed Reply with quote

On Mon, 9 Jan 2006 23:20:41 +1100, Gyruss wrote:
Quote:
Hi,

Given that the sed could output a multiline string when $OUTFILE has
multiple errors, is the code below acceptable? I mean is it ok to capture
a multiline string in a single variable if I just want to test whether or
not the string is null.

Cheers!

RESULT=`sed -e '/error/b' -e '/coredump/b' -e '/exception/b' -e d $OUTFILE`

Write it:

RESULT=`
sed -e '/error/q' -e '/coredump/q' -e '/exception/q' -e d < "$OUTFILE"
`

So that only the first matching line is returned and the
remaining not even processed (having a multiline result is not a
problem though).

Quote:

if [ -n $RESULT ] ; then

That should be:

if [ -n "$RESULT" ]; then

remember that leaving a variable expansion unquoted in shell has
a very special meaning.

Quote:
echo "Oh no!"
else
echo "Success"
fi


You could do:

if
awk '/error/ || /coredump/ || ... {
found = 1
exit
}
END {
exit (1 - found)
}' < "$OUTFILE"; then ...


--
Stephane
Back to top
Hubble
*nix forums beginner


Joined: 28 Nov 2005
Posts: 41

PostPosted: Mon Jan 09, 2006 3:38 pm    Post subject: Re: Emulating egrep with sed Reply with quote

Quote:
I mean is it ok to capture
a multiline string in a single >variable if I just want to test whether or
not the string is null.


Quote:
RESULT=`sed -e '/error/b' -e '/coredump/b' -e '/exception/b' -e d $OUTFILE`
if [ -n $RESULT ] ; then
...
Depends on your problem. If you know for sure that OUTFILE always

contains only a few lines, this is ok. If OUTFILE can span MBytes or
GBytes, certainly not ok.

Hubble.
Back to top
Gyruss
*nix forums beginner


Joined: 09 Jan 2006
Posts: 1

PostPosted: Mon Jan 09, 2006 12:20 pm    Post subject: Emulating egrep with sed Reply with quote

Hi,

Given that the sed could output a multiline string when $OUTFILE has
multiple errors, is the code below acceptable? I mean is it ok to capture
a multiline string in a single variable if I just want to test whether or
not the string is null.

Cheers!

RESULT=`sed -e '/error/b' -e '/coredump/b' -e '/exception/b' -e d $OUTFILE`

if [ -n $RESULT ] ; then
echo "Oh no!"
else
echo "Success"
fi
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Tue Jan 06, 2009 10:33 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts output grep or egrep to file without > because of | and x... jeolmeun shell 3 Fri Jun 02, 2006 6:16 pm
No new posts emulating action_replace_with_url() feature of MIMEDefang Kurt Lieber Postfix 2 Wed May 17, 2006 5:28 pm
No new posts egrep not working nasir shell 4 Thu May 04, 2006 8:36 pm
No new posts egrep question Stephanie Echevarria shell 4 Tue Apr 11, 2006 6:22 pm
No new posts grep,egrep,fgrep help needed jayend5000@yahoo.com shell 3 Fri Apr 07, 2006 12:22 pm

McDonalds | Bad Credit Credit Cards | Personal Loans | Loans | Carrinhos para bebĂȘ
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.5189s ][ Queries: 20 (0.4143s) ][ GZIP on - Debug on ]