LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2009, 04:57 AM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Rep: Reputation: 15
Find several different words and replace with one using sed.


So as my title says, im trying to match multiple words and replace them all with one.

I have tried "test|just|this" in my regex checker app and it matches all 3 words, but when I try use it in the sed command it doesn't match any...
What am I doing wrong here?

Code:
echo This is just quick little test | sed 's/test|just|this/./i'
I know I could just make several sed commands for each word but that seems messy and not conventional.

I would appreciate some help here.

Last edited by Techno Guy; 07-06-2009 at 05:03 AM.
 
Old 07-06-2009, 05:12 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Wrong tool for the job - spanners make poor screwdrivers.
 
Old 07-06-2009, 05:13 AM   #3
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by syg00 View Post
Wrong tool for the job - spanners make poor screwdrivers.
Ok, what would you suggest I use?
 
Old 07-06-2009, 05:15 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk
Code:
echo This is just quick little test | awk '{gsub(/test|just|this/,"")}1'
 
Old 07-06-2009, 05:19 AM   #5
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Fantastic, thanks ghostdog74.
That does exactly what I need it to do.

I did have a quick look at the awk man pages (at the examples) but it didn't really seem like the right thing at the time, but now I know it is.
 
Old 07-06-2009, 05:30 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Techno Guy View Post
Fantastic, thanks ghostdog74.
That does exactly what I need it to do.

I did have a quick look at the awk man pages (at the examples) but it didn't really seem like the right thing at the time, but now I know it is.
instead of the man pages, take a look at my sig for Gawk link.
 
Old 07-06-2009, 05:31 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Another happy customer - cheers ghostdog74.
Dammit, you're going to make everybody awk users ....
 
Old 07-06-2009, 06:07 AM   #8
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
I'v opened tabs on nearly all of the links in your sig

Just one more quick question,
How would I do a case insensitive match? (with sad I could just go /i but it's not so easy with awk as far as i'v seen)

I had a little read from the link in your sig...

I don't get how it work? or how im supposed to use it.
....
Code:
echo This is just quick little test | awk 'tolower($1) ~ {gsub(/test|just|this|\ /,".")}1'
???

Last edited by Techno Guy; 07-06-2009 at 06:09 AM.
 
Old 07-06-2009, 06:12 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
like i said , read the link. If you do, you will come across IGNORECASE. Set it to 1.
 
Old 07-06-2009, 06:13 AM   #10
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Well Sed also works take a look at this snippet
Code:
echo This is just quick little test | sed -e 's/test\|just\|this/./ig'


Output :
. is . quick little .
 
Old 07-06-2009, 06:25 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
Dammit, you're going to make everybody awk users ....

one of the things good about learning awk, is that you can totally skip learning sed, since their capabilities overlap (and awk is a small programming language itself)
 
Old 07-06-2009, 06:51 AM   #12
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
like i said , read the link. If you do, you will come across IGNORECASE. Set it to 1.
Yeah I did, but they also said
Quote:
"In general, you cannot use IGNORECASE to make certain rules case-insensitive and other rules case-sensitive, because there is no straightforward way to set IGNORECASE just for the pattern of a particular rule."
And since I may use awk as a case sensitive find&replace latter on, I would like to avoid changing things like that.

So, would you or someone else have any pointers as to how I would get it to work the other way (as stated in my previous post.)
 
Old 07-06-2009, 06:57 AM   #13
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Inspired by ghostdog :-)


Code:
echo This is just quick little test | awk 'IGNORECASE = 1 {gsub(/test|just|this/,".")}1'
 
Old 07-06-2009, 06:57 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by ghostdog74 View Post

one of the things good about learning awk, is that you can totally skip learning sed,
Funnily enough, I basically skipped awk and went straight from sed to perl.
Each to their own ...
 
Old 07-06-2009, 07:05 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Techno Guy View Post
And since I may use awk as a case sensitive find&replace latter on, I would like to avoid changing things like that.
then you simply toggle IGNORECASE before you search for the next pattern.
 
  


Reply

Tags
regex, sed



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
Multi-line find/replace with GnuWin32 Sed Question EdrickV Programming 4 04-26-2009 01:32 PM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 07:20 PM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 06:33 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:55 AM.

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
Open Source Consulting | Domain Registration