Link to home
Create AccountLog in
Avatar of 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.
Avatar of Jornak
Flag of Canada image

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

Open in new window

Avatar of 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.
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.
And if that doesn't work, force type out ALT+34 for your quotemarks.
Avatar of hongning2009


Retyping everything with the double quotes give me this error

Can't open *.fdf: Invalid argument.
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
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?
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).
That works one more small problem

What I need to replace


need to replace to


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.
Avatar of ozo
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
works great, just pointing out that this is case sensitive.  Thanks