-1

I'm trying to find a Perl on Windows code that enables me to apply multiple find and replace regex queries on multiple files in a specific directory. I'm able to apply multiple find and replace regex queries using many text editors (for example UltraEdit).

Omar
  • 6,681
  • 5
  • 21
  • 36

2 Answers2

3

Your question isn't very descriptive. Does this do what you want?

Edited to work on Windows

perl -i.bak -pe"BEGIN {@ARGV = map glob, @ARGV} s/a/b/g; s/c/d/g; s/e/f/g; " My/Files/Directory/*.txt

Update

Unless you have only a few changes to make it is probably best if you put the substitution commands in a file, like this

changes.pl

BEGIN {
    @ARGV = map glob("\"$_\""), @ARGV;
}

s/a/b/g;
s/c/d/g;
s/e/f/g;

Then you can run it on a set of files on the command line like this

perl -i.bak -p changes.pl My/Files/Directory/*.txt
Community
  • 1
  • 1
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Yes, this is what I'm trying to do. I'm trying to apply multiple find and replace queries for all the files in a specific directory using perl. I've tried this command line `perl -i -pe"s/a/b/g; s/c/d/g; s/e/f/g; " "My\Files\Directory\"*.txt` and the result was `Can't open My\Files\Directory\"*.txt: Invalid argument.` – Omar Aug 02 '15 at 17:56
  • @omareg94: Okay you're on WIndows. It always helps to add a Windows tag to your question if that's the case. I've changed my solution to show you a command line solution that will work, as well as a hybrid that lets you put all the substitutions into a source file – Borodin Aug 02 '15 at 19:13
  • @omareg94: By the way, don't change the forward slashes for backward ones -- even on Windows – Borodin Aug 02 '15 at 19:14
  • It runs well now. Just a remaining issue. How could I mention folders that're having a space in their names? for example `E:\My\Folder 1\Folder 2`. When I add the double quotes it gives error. – Omar Aug 02 '15 at 19:57
  • I've reached the solution for the containing space directory thanks to [Ed Guiness](http://stackoverflow.com/questions/4285989/in-perl-how-to-handle-filenames-with-spaces). Now the script works as charm using this syntax `perl -i.bak -p changes.pl "\"E:/My/Folder 1/Folder 2/\""*.txt` – Omar Aug 02 '15 at 20:39
  • A last question. How could I apply changes to the files without creating backup files? – Omar Aug 02 '15 at 20:40
  • @omareg94: It's much neater to add the double quotes in `changes.pl`. I've modified my answer to show how. The command line becomes just `perl -i.bak -p changes.pl "E:/My/Folder 1/Folder 2/*.txt"` – Borodin Aug 02 '15 at 22:56
  • @omareg94: You can't use the `-i` (edit in-place) option on Windows without a parameter, which would be the right way to avoid making a backup. What you could do is to put all the backups in a subdirectory (by something like `-i"save/*"`) so that it's simple to delete them. Or it could be done by programming instead of using the command-line option. But your problem has grown substantially since your original question, and what was originally a good approach us no longer relevant. I suggest you ask a new question about this – Borodin Aug 02 '15 at 23:04
  • Please take a look at this [question](http://stackoverflow.com/questions/32469663/save-backup-files-to-custom-directory-in-perl-edit-in-place) as `-i"save/*"` doesn't work for me. – Omar Sep 09 '15 at 09:18
1

Here is a basic perl program (perhaps name it myreplacers.pl) that can do that:

#!/usr/bin/perl

use strict;
use warnings;

while(<>) {
  s/regex1/replace1/;
  s/regex2/replace2/;
  print;
}

Alter this with appropriate regular expressions and replacement texts and run it like perl myreplacers.pl file1 file2 etc.

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
  • how could I tell it to apply the replace on all the files in a specific directory? – Omar Aug 02 '15 at 17:32
  • `perl myreplacers.pl *` will do that for all files in a specific directory, unless the directory is really really big, in which case use `find . -maxdepth 1 -print0 |xargs -0 perl myreplacers.pl` – Adam Katz Aug 02 '15 at 17:34
  • I've run the command line `perl myreplacers.pl "My\Files\Directory\"*.txt` The result is `Can't open My\Files\Directory\"*.txt: Invalid argument at myreplacers.pl line 5.` – Omar Aug 02 '15 at 17:51
  • Oh, Windows. Sorry about that. It'd be easiest to skip the directory; change directories first (`cd "My\Files\Directory"`) and then run `perl myreplacers.pl *.txt` – Adam Katz Aug 03 '15 at 01:46