Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do bulk search and replace with Perl?

Tags:

replace

perl

bulk

I have the following script that takes in an input file, output file and replaces the string in the input file with some other string and writes out the output file.

I want to change the script to traverse through a directory of files i.e. instead of prompting for input and output files, the script should take as argument a directory path such as C:\temp\allFilesTobeReplaced\ and search for a string x and replace it with y for all files under that directory path and write out the same files.

How do I do this?

Thanks.

$file=$ARGV[0];

open(INFO,$file);
@lines=<INFO>;
print @lines;

open(INFO,">c:/filelist.txt");

foreach $file (@lines){
   #print "$file\n";
   print INFO "$file";
}

#print "Input file name: ";
#chomp($infilename = <STDIN>);

if ($ARGV[0]){
   $file= $ARGV[0]
}

print "Output file name: ";
chomp($outfilename = <STDIN>);
print "Search string: ";
chomp($search = <STDIN>);
print "Replacement string: ";
chomp($replace = <STDIN>);

open(INFO,$file);
@lines=<INFO>;
open(OUT,">$outfilename") || die "cannot create $outfilename: $!";

foreach $file (@lines){    
    # read a line from file IN into $_
    s/$search/$replace/g; # change the lines
    print OUT $_; # print that line to file OUT
}
close(IN);
close(OUT);
like image 471
shubster Avatar asked May 27 '09 21:05

shubster


People also ask

How do I search and replace in Perl?

Performing a regex search-and-replace is just as easy: $string =~ s/regex/replacement/g; I added a “g” after the last forward slash. The “g” stands for “global”, which tells Perl to replace all matches, and not just the first one.

Which function is used for handling replacing expressions in Perl?

Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user.

How do I search for a word in a file in Perl?

Use of Wild Cards in Regular Expression: Perl allows to search for a specific set of words or the words that follow a specific pattern in the given file with the use of Wild cards in Regular Expression. Wild cards are 'dots' placed within the regex along with the required word to be searched.

How do I replace a substring in a string in Perl?

Examples of Perl replace In the above example, we used the string characters in one separate variable, and we will use some string default methods like substr(); it will be used to find the characters in the strings, and it will be replaceable by other characters.


2 Answers

The use of the perl single liner

perl -pi -e 's/original string/new string/' filename

can be combined with File::Find, to give the following single script (this is a template I use for many such operations).

use File::Find;

# search for files down a directory hierarchy ('.' taken for this example)
find(\&wanted, ".");

sub wanted
{
    if (-f $_)
    {
        # for the files we are interested in call edit_file().
        edit_file($_);
    }
}

sub edit_file
{
    my ($filename) = @_;

    # you can re-create the one-liner above by localizing @ARGV as the list of
    # files the <> will process, and localizing $^I as the name of the backup file.
    local (@ARGV) = ($filename);
    local($^I) = '.bak';

    while (<>)
    {
        s/original string/new string/g;
    }
    continue
    {
        print;
    }
}
like image 91
Beano Avatar answered Oct 03 '22 10:10

Beano


You can do this with the -i param:

Just process all the files as normal, but include -i.bak:

#!/usr/bin/perl -i.bak

while ( <> ) {
   s/before/after/;
   print;
}

This should process each file, and rename the original to original.bak And of course you can do it as a one-liner as mentioned by @Jamie Cook

like image 25
chris Avatar answered Oct 03 '22 11:10

chris