4

I want to replace a string with another string in all the files in windows. All files with same extension. I tried with following perl one liner command but it seems "*" is not working for windows.

perl.exe -pi.bak -e "s/Hello/ABCD/ge" C:\Users\Jamimb\Desktop\Test\*.tmp

So please help.

5
  • 1
    I suspect the equivalent works in Linux because the command interpreter shell expands any filename with a * to a list of the actual filenames, before the program is executed. It may not work in Windows because this wildcard expansion of the * does not occur. Commented Jul 15, 2013 at 10:48
  • @Paul:Any other way instead of * in windows? Commented Jul 15, 2013 at 10:51
  • 1
    The perl might behave better if run from cygwin or some other shell instead of the usual dos command line. cygwin.com Commented Jul 15, 2013 at 10:53
  • Before trying that, check and see if your perl will modify individual files. If it won't modify individual files that you name, there's no point fixing the *. Commented Jul 15, 2013 at 10:56
  • @Paul: "Behave like Linux" is not a synonym for "Behave better". There are many good things about Linux, but having any parameter that looks like a file glob expanded for you before you can look at it isn't one of them. Commented Jul 15, 2013 at 11:09

2 Answers 2

2

You can expand the command line parameter explicitly from within Perl using glob.

Also, the /e ("eval") modifier on the substitution is wrong in this instance. If strict were in place it would cause a Bareword not allowed error, but without it it is simply a no-op as ABCD evaluates as 'ABCD'.

Try this

perl -i.bak -pe "BEGIN{@ARGV = map glob, @ARGV} s/Hello/ABCD/g" C:\Users\Jamimb\Desktop\Test\*.tmp
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much..The above applies to the files which are in same directory. Is there any way to replace the string in files of sub directory?
I'm not clear what you mean. It will use whatever directory is specified in the command-line parameter. It will also expand several wildcarded expressions if you pass more than one parameter, like perl ... path1/*.tmp path1/subdir/*.tmp
0

You can try this (perhaps):

for %f in (C:\Users\Jamimb\Desktop\Test\*.tmp) do perl.exe -pi.bak -e "s/Hello/ABCD/g" %f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.