Sed programming question

Cameron Simpson cs at zip.com.au
Thu Dec 11 02:47:26 UTC 2008


On 10Dec2008 18:39, Dan Thurman <dant at cdkkt.com> wrote:
> I think sed can handle relational operators, but I have not
> been able to figure it out. Am I correct in assuming that
> sed can use relational operators such as OR (|) or AND
> (&)?
>
> Here is an example, but does not work:
>
> echo "The" | sed -e '/(the)|(THE)|(The)/i\ GOOD!'
>
> I tried looking up examples on the Internet, but could
> not find it.  My brain is getting real mushy.

Sed doesn't directly do relational operators.
_If_ you are using GNU sed _and_ you use the -r option, then you have
"extended/full regular expressions", and they support "|" (alternation,
like OR).

"&" is not AND, it's a substitution syntax for "what was matched by the
left hand regexp", eg:

  s/bill/& and ted/

You can only do AND by nesting multiple pattern matches:

  /foo/{
    /bar/{
      ... do stuff for "foo" AND "bar"
    }
  }

although you can fake it at some performance expense thus:

  /(foo.*bah|bah.*foo)/{
    ... do stuff for "foo" AND "bar"
  }

This gets combinatorially worse for each additional AND you try to fake;
you are better off nesting matches as in the previous example.

Have you considered joining the sed-users list?

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Frisbeetarianism: The belief that when you die, your soul goes up on the roof
and gets stuck. - Tony Molina <amolina at ozemail.com.au>




More information about the users mailing list