Unix Shell - Emulating egrep with sed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Emulating egrep with sed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Emulating egrep with sed
Gyruss

2006-01-09, 7:51 am

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


Hubble

2006-01-09, 6:02 pm

>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.



>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.

Stephane Chazelas

2006-01-09, 6:02 pm

On Mon, 9 Jan 2006 23:20:41 +1100, Gyruss wrote:
> 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).

>
> 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.

> echo "Oh no!"
> else
> echo "Success"
> fi



You could do:

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


--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com