LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find several different words and replace with one using sed. (https://www.linuxquestions.org/questions/linux-newbie-8/find-several-different-words-and-replace-with-one-using-sed-738011/)

Techno Guy 07-06-2009 04:57 AM

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.

syg00 07-06-2009 05:12 AM

Wrong tool for the job - spanners make poor screwdrivers.

Techno Guy 07-06-2009 05:13 AM

Quote:

Originally Posted by syg00 (Post 3598234)
Wrong tool for the job - spanners make poor screwdrivers.

Ok, what would you suggest I use?

ghostdog74 07-06-2009 05:15 AM

use awk
Code:

echo This is just quick little test | awk '{gsub(/test|just|this/,"")}1'

Techno Guy 07-06-2009 05:19 AM

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.

ghostdog74 07-06-2009 05:30 AM

Quote:

Originally Posted by Techno Guy (Post 3598246)
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.

syg00 07-06-2009 05:31 AM

Another happy customer - cheers ghostdog74.
Dammit, you're going to make everybody awk users .... :p

Techno Guy 07-06-2009 06:07 AM

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'
???

ghostdog74 07-06-2009 06:12 AM

like i said , read the link. If you do, you will come across IGNORECASE. Set it to 1.

PMP 07-06-2009 06:13 AM

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 .


ghostdog74 07-06-2009 06:25 AM

Quote:

Originally Posted by syg00 (Post 3598265)
Dammit, you're going to make everybody awk users .... :p

:)
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)

Techno Guy 07-06-2009 06:51 AM

Quote:

Originally Posted by ghostdog74 (Post 3598309)
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.)

PMP 07-06-2009 06:57 AM

Inspired by ghostdog :-)


Code:

echo This is just quick little test | awk 'IGNORECASE = 1 {gsub(/test|just|this/,".")}1'

syg00 07-06-2009 06:57 AM

Quote:

Originally Posted by ghostdog74 (Post 3598324)
:)
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 ...

ghostdog74 07-06-2009 07:05 AM

Quote:

Originally Posted by Techno Guy (Post 3598368)
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.

Techno Guy 07-06-2009 07:07 AM

Quote:

Originally Posted by PMP (Post 3598374)
Inspired by ghostdog :-)


Code:

echo This is just quick little test | awk 'IGNORECASE = 1 {gsub(/test|just|this/,".")}1'

Code:

# echo This is just quick little test | awk 'IGNORECASE = 1 {gsub(/test|just|this/,".")}1'
This is . quick little

.

It doesn't work. (running Debian 5
Code:

# awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

Edit:
Never mind, I just updated it, and it works now!
Thanks!
Code:

# awk -W version
GNU Awk 3.1.6


PMP 07-06-2009 07:14 AM

Well Techno Guy,
It worked for me
I am beginner to awk ghostdog might answer your quesiton, you can use sed option that i have given or

Code:

echo This is just quick little test | perl -e 'while (<>) { s/this|just|test/./ig; print}'

ghostdog74 07-06-2009 07:14 AM

IGNORECASE is gawk feature. if there is no way you can use gawk, then no choice, use tolower()

Techno Guy 07-06-2009 07:16 AM

Quote:

Originally Posted by ghostdog74 (Post 3598394)
IGNORECASE is gawk feature. if there is no way you can use gawk, then no choice, use tolower()

Yeah I just realized, I just installed gawk :)
All works fine now.

Thanks so much for your help guys.
You guys have overwhelmed me with options :)
Until next time :)


All times are GMT -5. The time now is 06:47 PM.