Can sed match a choice of two?

Can sed match a choice of two?

am 24.04.2008 01:32:58 von simonp

I have a sed script that I need to add a pattern matching one of
two choices (.rar or .zip). I know this can be done in awk and
egrep with (rar|zip), but is there anyway to get this behaviour
in sed?

Do I have to rewrite my script in perl?

Cheers,
Simon

Re: Can sed match a choice of two?

am 24.04.2008 01:51:26 von mop2

don't work /zip\|rar/ with sed?


sim...@nospam.com wrote:
> I have a sed script that I need to add a pattern matching one of
> two choices (.rar or .zip). I know this can be done in awk and
> egrep with (rar|zip), but is there anyway to get this behaviour
> in sed?
>
> Do I have to rewrite my script in perl?
>
> Cheers,
> Simon

Re: Can sed match a choice of two?

am 24.04.2008 02:13:10 von Rajan

wrote in message news:fuogva$ejs$1@reader2.panix.com...
> I have a sed script that I need to add a pattern matching one of
> two choices (.rar or .zip). I know this can be done in awk and
> egrep with (rar|zip), but is there anyway to get this behaviour
> in sed?
>
> Do I have to rewrite my script in perl?
>
> Cheers,
> Simon
>

I use gnu sed (if I have to) and I can say -r to make sed understand
extended regex. Alternatively, I can write it the script twice with -e with
different regexs (I won't do it). Calling sed experts....

Re: Can sed match a choice of two?

am 24.04.2008 03:20:52 von Maxwell Lol

simonp@nospam.com writes:

> Do I have to rewrite my script in perl?

It's not too bad if you have to, using s2p (part of perl).

Re: Can sed match a choice of two?

am 24.04.2008 04:05:28 von simonp

Rajan wrote:
>
>
> wrote in message news:fuogva$ejs$1@reader2.panix.com...
>> I have a sed script that I need to add a pattern matching one of
>> two choices (.rar or .zip). I know this can be done in awk and
>> egrep with (rar|zip), but is there anyway to get this behaviour
>> in sed?
>>
>> Do I have to rewrite my script in perl?
>>
>> Cheers,
>> Simon
>>
>
> I use gnu sed (if I have to) and I can say -r to make sed understand
> extended regex. Alternatively, I can write it the script twice with -e with
> different regexs (I won't do it). Calling sed experts....
>

This is the solution. The -r option (-E in my FreeBsd shell
account) enables extended pattern regex.

I actually looked this up in Linux in a Nutshell from O'Reilly.
They have a grid with all the utilities like ed, awk, grep, sed
and what they can do. Extended regex was *not* tagged for sed,
though it was for grep. (Though it is there in the dedciated
chapter on sed). Must be an error.

Thanks again,
Simon

Re: Can sed match a choice of two?

am 24.04.2008 04:30:02 von simonp

mop2 wrote:
> don't work /zip\|rar/ with sed?
>
>
> sim...@nospam.com wrote:
>> I have a sed script that I need to add a pattern matching one of
>> two choices (.rar or .zip). I know this can be done in awk and
>> egrep with (rar|zip), but is there anyway to get this behaviour
>> in sed?
>>
>> Do I have to rewrite my script in perl?
>>
>> Cheers,
>> Simon

This is what I go to work:

s/(rar|zip|txt|jpg) +- +/\1 ::INFO:: /g

The match in the group of file extensions is stored, then kept,
with extra text appended.

Is there any consistently in what has go be escaped in extended
regexp? The () did not need to be escaped here, but to use what
was stored you still need the escape \1.

-Simon

Re: Can sed match a choice of two?

am 24.04.2008 05:29:58 von Ed Morton

On 4/23/2008 6:32 PM, simonp@nospam.com wrote:
> I have a sed script that I need to add a pattern matching one of
> two choices (.rar or .zip). I know this can be done in awk and
> egrep with (rar|zip), but is there anyway to get this behaviour
> in sed?
>
> Do I have to rewrite my script in perl?

Apparently not because, as you said:

> I know this can be done in awk and
>> egrep with (rar|zip)

Ed.

Re: Can sed match a choice of two?

am 24.04.2008 05:34:57 von Ed Morton

On 4/23/2008 9:30 PM, simonp@nospam.com wrote:
> mop2 wrote:
>
>>don't work /zip\|rar/ with sed?
>>
>>
>>sim...@nospam.com wrote:
>>
>>>I have a sed script that I need to add a pattern matching one of
>>>two choices (.rar or .zip). I know this can be done in awk and
>>>egrep with (rar|zip), but is there anyway to get this behaviour
>>>in sed?
>>>
>>>Do I have to rewrite my script in perl?
>>>
>>>Cheers,
>>>Simon
>>
>
> This is what I go to work:
>
> s/(rar|zip|txt|jpg) +- +/\1 ::INFO:: /g
>
> The match in the group of file extensions is stored, then kept,
> with extra text appended.
>
> Is there any consistently in what has go be escaped in extended
> regexp?

Yes.

> The () did not need to be escaped here, but to use what
> was stored you still need the escape \1.
>
> -Simon

The \1 isn't part of an extended regexp, it's the sed operator to backreference
part of a matched regexp. In GNU awk the equivalent is \\1.

Ed.

Re: Can sed match a choice of two?

am 24.04.2008 19:29:17 von Dave B

On Thursday 24 April 2008 01:32, simonp@nospam.com wrote:

> I have a sed script that I need to add a pattern matching one of
> two choices (.rar or .zip). I know this can be done in awk and
> egrep with (rar|zip), but is there anyway to get this behaviour
> in sed?

If you /absolutely/ need to do that with standard sed, then you can do
something like (depending upon what you want to do after the match,
anyway):

/\.rar/b found
/\.zip/b found

....

:found
....


However, I think you better do that with GNU sed, if you can, or use another
tool. You've already got good answers.

--
D.