Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

grouping in sed with OR operator in bash in one line

5,470 views
Skip to first unread message

Toby

unread,
Jul 20, 2010, 1:50:06 PM7/20/10
to
Hi,

I would like to replace a a set of words by another word via sed. My
first try was:

echo "a cat or a dog" | sed 's/\(cat\)\|\(dog\)/PET/g'

I also tried some variations of this. But all failed.

For any help I would be very happy.

Cheers,
Toby

P.S. some variations of the search pattern
\(cat|dog\)
\(\(cat\)\|\(dog\)\)

Alan Curry

unread,
Jul 20, 2010, 5:02:10 PM7/20/10
to
In article <caa6a54d-8488-499c...@g35g2000yqa.googlegroups.com>,

Toby <tobias...@gmail.com> wrote:
>Hi,
>
>I would like to replace a a set of words by another word via sed. My
>first try was:
>
> echo "a cat or a dog" | sed 's/\(cat\)\|\(dog\)/PET/g'
>
>I also tried some variations of this. But all failed.

Works for me:

$ echo "a cat or a dog" | sed 's/\(cat\)\|\(dog\)/PET/g'
a PET or a PET

The \(...\) aren't necessary, since the \| has low precedence.

$ echo "a cat or a dog" | sed 's/cat\|dog/PET/g'
a PET or a PET

$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

--
Alan Curry

Toby

unread,
Jul 22, 2010, 1:29:19 PM7/22/10
to
On Jul 20, 10:02 pm, pac...@kosh.dhis.org (Alan Curry) wrote:
> In article <caa6a54d-8488-499c-8057-f24e930f3...@g35g2000yqa.googlegroups.com>,
Hi Alan,

indeed I had the BSD version. I followed this to install GNU. Now it
works.

Thanks a lot.

Cheers,
Toby

References
[1] http://dsmarkchen.blogspot.com/2007/12/gnu-sed-on-mac.html

0 new messages