Avatar of hongning2009
hongning2009
 asked on

What is wrong? PERL search and replace

I am new in PERL and testing the following command in windows Strawberry

perl.exe -pi.bak -e "BEGIN{@ARGV=<*.fdf>} s/FP944/F951/g"

There is only one file that has extension fdf and I receive this error

The filename, directory name, or volume label syntax is incorrect.

The command above supposedly work in windows, anyway I tested this command also

perl -pi.bak -e "s/FP944/FP951/g" *.fdf

and I get the following error
Unrecognized character \x93; marked by <-- HERE after <-- HERE near column 1 at -e line 1.


What I want accomplish is to replace a string  FP944 with FP955 and do it from that directory and continuing to the subdirectories.

Any help would be great.
PerlScripting LanguagesMicrosoft Legacy OS

Avatar of undefined
Last Comment
hongning2009

8/22/2022 - Mon
Jornak

Use single quotes instead of doubles:
perl -pi.bak -e 's/FP944/FP951/g' *.fdf

Open in new window

wilcoxon

Unless you are on Microsoft Windows (which I'm guessing the original author is based on his topic tags).  Windows must use double quotes around the perl code (single quotes simply do not work on Windows).

If you want to recurse into subdirectories, you'll need to write a script.  The simplest way is to start with "find2perl . -name \*.fdf -exec replace_here \;" and then edit the resulting script.
Jornak

Make sure you're not copy-pasting your code from anywhere and typing it out yourself by hand as \0x93 is a Left double quote which isn't recognized by perl.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jornak

And if that doesn't work, force type out ALT+34 for your quotemarks.
hongning2009

ASKER
Retyping everything with the double quotes give me this error

Can't open *.fdf: Invalid argument.
ozo

Windows shell does not do file glob expansion.
In windows, you would need to let perl do the glob to set @ARGV, as in the first version
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hongning2009

ASKER
so does this mean that this command should work?

perl.exe -pi.bak -e "BEGIN{@ARGV=<*.fdf>} s/FP944/F951/g"

whats wrong with it?
wilcoxon

Yes.  That command should work fine under Strawberry Perl.  I just ran a very similar command under DWIM Perl (Strawberry Perl with some extra modules and utilities).

perl -pi.bak -e "BEGIN {@ARGV=<*.txt>} s/CASEI/CASE2/g"

Open in new window


ran fine and did what was expected.  Note that it will only run in the current directory - if you want it to recurse into sub-directories you must do something more complex (run manually in each, use File::Find, etc).
hongning2009

ASKER
That works one more small problem

What I need to replace

IMAGE_FILE=\\FP9xx\FormDB_TRAIN1\

need to replace to

IMAGE_FILE=\\FP9yx\FormDB_TRAIN2\

When I run this

perl.exe -pi.bak -e "BEGIN{@ARGV=<*.fdf>} s/IMAGE_FILE=\\FP9xxFormDB_TRAIN1\/IMAGE_FILE=\\FP9yx\FormDB_TRAIN2\/g"

I get

Substitution pattern not terminated at -e line 1.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
wilcoxon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hongning2009

ASKER
works great, just pointing out that this is case sensitive.  Thanks