LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed: howto group regex's into AND/OR clauses? (https://www.linuxquestions.org/questions/linux-software-2/sed-howto-group-regexs-into-and-or-clauses-573961/)

jhwilliams 08-01-2007 11:47 AM

sed: howto group regex's into AND/OR clauses?
 
OR and AND are hard to google for...

Is there anyway to change something like:

Code:

echo "one two three four" | \
sed "s/regex_for_one/substitution_string/" | \
sed "s/regex_for_two/substitution_string/" | \
sed "s/regex_for_three/substitution_string/" | \
sed "s/regex_for_four/substitution_string/"

into something like this...
Code:

echo "one two three four" | \
sed "s/regex_for_one OR regex_for_two OR regex_for_three OR regex_for_four/substitution_string/"

This is a question about syntax, basically. How do I combine regular expressions into and/or groups, given that the above capitalized OR's doesn't work?

AlucardZero 08-01-2007 12:40 PM

Code:

alucard@organa:~$ echo "one two three four" | sed -r "s/(one|two|three|four)/five/g"
five five five five

-r for "extended regular expressions"; (A|B) meaning A OR B

makyo 08-01-2007 01:00 PM

Hi.

This single sed invocation in this script:
Code:

#!/bin/sh

# @(#) s1      Demonstrate sed substitution.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
sed --version | head -1 >&2
echo

echo "one two three four" |
sed \
-e "s/one/ONE/" \
-e "s/two/TWO/" \
-e "s/three/THREE/" \
-e "s/four/FOUR/"

exit 0

produces:
Code:

% ./s1

GNU bash 2.05b.0(1)-release
GNU sed version 4.1.2

ONE TWO THREE FOUR

If this is not what you had in mind, please explain more ... cheers, makyo

jhwilliams 08-01-2007 01:02 PM

Quote:

(one|two|three|four)
Ah yes. That would make good sense, indeed. Thanks!

makyo 08-01-2007 01:05 PM

Hi.

I think AlucardZero understood better than I did ... cheers, makyo

jhwilliams 08-01-2007 02:19 PM

Quote:

Hi.

I think AlucardZero understood better than I did ... cheers, makyo
Yes, I believe so - it is hard to ask understandable questions! Thank you for the help, anyhow.

- Jhwilliams


All times are GMT -5. The time now is 01:44 AM.