Apply multiple find and replace regex queries on multiple .txt files using a Perl code

前端 未结 2 1257
别跟我提以往
别跟我提以往 2020-12-12 08:09

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 mul

相关标签:
2条回答
  • 2020-12-12 09:09

    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
    
    0 讨论(0)
  • 2020-12-12 09:10

    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.

    0 讨论(0)
提交回复
热议问题