LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 08-01-2007, 12:47 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 208Reputation: 208Reputation: 208
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?
 
Old 08-01-2007, 01:40 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,652

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
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
 
Old 08-01-2007, 02:00 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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
 
Old 08-01-2007, 02:02 PM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
(one|two|three|four)
Ah yes. That would make good sense, indeed. Thanks!
 
Old 08-01-2007, 02:05 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

I think AlucardZero understood better than I did ... cheers, makyo
 
Old 08-01-2007, 03:19 PM   #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 208Reputation: 208Reputation: 208
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
 
  


Reply

Tags
regex, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 07:07 AM
howto change GROUP permissions (have read/googled for hours) arjay Debian 8 08-09-2005 11:40 AM
howto add users to group? Lleb_KCir Linux - General 2 06-04-2005 01:28 AM
Howto group map in Samba 2.2.x rizza Linux - Networking 0 01-23-2004 06:30 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 09:28 PM


All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration