Better way to do "echo $x | sed ..." and "echo $x | grep ..."

Posted by DevSolar on Super User See other posts from Super User or by DevSolar
Published on 2010-02-04T10:11:29Z Indexed on 2010/05/19 4:10 UTC
Read the original article Hit count: 147

Filed under:
|
|

I often find this in scripts (and, I have to admit, write it myself):

a=`echo $x | sed "s/foo/bar/"`

or

if echo $x | grep foo
then
    ...
fi

Consider "foo" to include some regex stuff.

I feel that there should be - and most likely is - a better way to phrase this, one that does not involve two commands and a pipe but wraps the thing into some more compact expression.

I just can't find it. Anybody?

© Super User or respective owner

Related posts about shell

Related posts about grep