Sed programming question

Dan Thurman dant at cdkkt.com
Thu Dec 11 03:01:24 UTC 2008


Cameron Simpson wrote:
> 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,
>   
Wow, thanks a lot!  I certainly realize now that relational operators
are quite limited with sed, which is why there weren't any examples
offered over the Internet!  Duh oh!

Thanks for the tip regarding joining the sed-users group!

Dan




More information about the users mailing list