Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive search and replace usind Perl in cmd (Windows)

Tags:

cmd

perl

I am using this command to search and replace a string with another in the command prompt:

 perl -pi -i.bak -e "s/Mohan/Sitaram/g" ab.txt

This replaces Mohan with Sitaram in the file ab.txt in the current directory.

However I want to replace all occurrences of Mohan with Sitaram in all .txt files in all the sub-directories (recursively). Using *.txt instead of ab.txt doesn’t work. Regular expressions work otherwise as I have downloaded the regex packages for Windows. It doesn’t work only for this command saying

E:\>perl -pi -e "s/Sitaram/Mohan/g" *.txt
Can't open *.txt: Invalid argument.

Is there any way to fix this? A different command perhaps?

like image 895
Mohan Sitaram Avatar asked Mar 30 '11 06:03

Mohan Sitaram


1 Answers

find . -name "*.txt" | xargs perl -p -i -e "s/Sitaram/Mohan/g"

find is used to search all *.txt files recursively.

xargs is used to build and execute command lines from standard input.

like image 76
josh Avatar answered Oct 13 '22 09:10

josh