All pastes #1767097 Raw Edit

perl remove badstring from file

public text v1 · immutable
#1767097 ·published 2010-01-26 21:32 UTC
rendered paste body


#!/usr/local/bin/perl

### called with:    ~/cleanup.pl {badstring} [filenames ...]

use Tie::File;

my $badstring=shift @ARGV;
my $numArgs = $#ARGV + 1;
my $file_name;

print "removing $badstring from $numArgs files\n";

foreach $argnum (0 .. $#ARGV)
{
        $file_name = $ARGV[$argnum];
        print "\nprocessing $ARGV[$argnum]  ";

        # open the file with tie
        tie my @file_lines, 'Tie::File', $file_name or die;

        # filter out lines containing 'sandwich'
        @file_lines = grep { !/$badstring/ } @file_lines;

        # close the file with untie.
        # IMPORTANT: always untie when you are done!
        untie @file_lines or die "$!";
        print " ... done";
}
print "\n";