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.
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.
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.
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.
ASKER
Retyping everything with the double quotes give me this error
Can't open *.fdf: Invalid argument.
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
In windows, you would need to let perl do the glob to set @ARGV, as in the first version
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?
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).
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).
perl -pi.bak -e "BEGIN {@ARGV=<*.txt>} s/CASEI/CASE2/g"
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).
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\/I MAGE_FILE= \\FP9yx\Fo rmDB_TRAIN 2\/g"
I get
Substitution pattern not terminated at -e line 1.
What I need to replace
IMAGE_FILE=\\FP9xx\FormDB_
need to replace to
IMAGE_FILE=\\FP9yx\FormDB_
When I run this
perl.exe -pi.bak -e "BEGIN{@ARGV=<*.fdf>} s/IMAGE_FILE=\\FP9xxFormDB
I get
Substitution pattern not terminated at -e line 1.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
works great, just pointing out that this is case sensitive. Thanks
Open in new window