PDA

View Full Version : Perl Search / replace trouble


toneharb
11-20-2005, 03:48 PM
I find this command on LINUX useful for global searches and replacings

perl -pi -e 's/search/replace/g' filename(s)

I am having a spot of trouble though with replacing a string which contains the literal single apostrophe ' character

ie: 'string'

I have tried perl -pi -e 's/\'string\'/replace/g' filenames

and have also atempted perl -pi -e 's/\o47string\o47/replace/g' filenames

The command interpreter seems to think its a multi-line command for (1) and for (2) it doesn't find a match so octal 47 escape sequence does seem to mean '

Any Perl command line gurus know the solution to this Unix problem ?

FishMonger
11-20-2005, 04:21 PM
perl -pi -e s/\'string\'/replace/g filenames

Should have worked, it did for me.

toneharb
11-20-2005, 05:00 PM
Thanks Fishmonger

Confirms i was doing the right thing then , but when i do it i get

>

symbol on pressing return as though it wants another line of input - ie: it doesn't execute the command line ...maybe it is working - is there some other key combination i have to press ie: not return key i wonder ? Sorry i'm a complete Unix novice! (used it about 20 years but still learning how to use it!)

toneharb
11-20-2005, 05:29 PM
Replying to myself lol !

Just though i'd let you know - found a way around

perl -pi -e "s/\'string\'/replace/g" filenames



ie: used double quotes instead of single ! after s and g flags.

FishMonger
11-20-2005, 05:39 PM
Are you quoting the regex command?

[rkb@iso102 rkb]$ uname -o
GNU/Linux
[rkb@iso102 rkb]$ cat test
This 'is' a test

[rkb@iso102 rkb]$ perl -pi -e 's/\'is\'/was/g' test
>
[rkb@iso102 rkb]$ perl -pi -e s/\'is\'/was/g test
[rkb@iso102 rkb]$ cat test
This was a test


[rkb@iso102 rkb]$ cat test
This 'is' a test

[rkb@iso102 rkb]$ perl -pi -e "s/\'is\'/was/g" test
[rkb@iso102 rkb]$ cat test
This was a test