Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl one line string replacement (mulitple?)

Status
Not open for further replies.

howdthattaste

Programmer
Apr 12, 2007
17
US
hello

is there a way to do multiple string replacements in one line?

sentence:
"The dog ran into the dog store."

rule: Always replace 'dog' with 'cat' and always replace 'store' with 'shop'

my guess:
$string =~ s/dog/cat/store/shop/g;

something like that... I guess, if its possible, im looking for the syntax, maybe some paraenthesis?
 
You could have a hash of words you want to replace and build your regexp from that, like so:

Code:
my $string = "The dog ran into the dog store.";

my %replacements = (
   dog => 'cat',
   store => 'shop'
);

my $regexp = '(' . join( '|', keys %replacements ) . ')';

$string =~ s/$regexp/$replacements{$1}/go;

print "$string\n";
 
right, thanks. but i was looking for a one-liner type approach.
via a script, since its just two items, i could use a list. but since its only 2, i feel it falls on that line between one-liner or script. i really dont want to have to write another script.

but anyway, thank you very much for the hash approach, ive dont things like this in the past with larger lists, and this is def a shortcut!


paul
 
one line or one regexp? This is one line:

Code:
$string =~ s/dog/cat/g && $string =~ s/store/shop/g;

There is no need to do it any other way and using a more complex regexp is probably less efficient anyway.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
yeah, that works. i was actually looking for a one-line, one regexp. i thought there might be a shorter syntax than using "&&", but i guess thats efficient enough. thanks guys!

"Not New York, Kansas
 
If you use the && operator, the second replacement will only be attempted if the first causes at least one replacement (as it returns 0 otherwise). Just use them as two separate statements:
Code:
$string =~ s/dog/cat/g;
$string =~ s/store/shop/g;
 
oh! good point. but two separate statements wouldn't be a one-liner would it?

im using windows so it would be like this:

c:\> perl -nle "print if s/dog/cat/g;" text.txt > new.txt


but, how do i do both dog/cat as well as store/shop is what im asking.


"Not New York, Kansas
 
nice!
i probably should said that from the start. my first attempt in my original post was very close to feherke's. duh. semicolon.

anyway, thanks, this all leads up to my next question, which is how do i do this:

c:\> perl -ple "s/dog/cat/g;s/store/shop/g" *.txt > new.txt

on multiple files? i guesss i'd need >> to append as well?


"Not New York, Kansas
 
thanks for the link. actually, its pretty easy now that i think about it months later:

Code:
c:\> type *.txt | perl -ple "s/dog/cat/g;"

that webpage is very technical, but i call my verion the 'type and pipe'. it gets the job done, but the type command prints the filenames and some extra newlines, whatta-ya-gonna-do. (maybe 'type' has some secret switch to combat that?)

thanks, i hope this helps someone somehow, i cant even remember why i needed to do it.

----------------------------------
"Not New York..., Kansas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top