Skip to content

Instantly share code, notes, and snippets.

@luisfaceira
Created September 15, 2011 17:59
Show Gist options
  • Save luisfaceira/1219977 to your computer and use it in GitHub Desktop.
Save luisfaceira/1219977 to your computer and use it in GitHub Desktop.
Perl script to remove lines with a certain pattern in a file or list of files - originally from http://stackoverflow.com/users/47529/chaos
#!/usr/bin/perl
use IO::Handle;
my $pat = shift(@ARGV) or
die("Usage: $0 pattern files\n");
die("Usage $0 pattern files\n")
unless @ARGV;
foreach my $file (@ARGV) {
my $io = new IO::Handle;
open($io, $file) or
die("Cannot read $file: $!\n");
my @file = <$io>;
close($io);
foreach my $line (@file) {
if($line =~ /$pat/o) {
$line = '';
$found = 1;
last;
}
}
if($found) {
open($io, ">$file") or
die("Cannot write $file: $!\n");
print $io @file;
close($io);
}
}
@luisfaceira
Copy link
Author

I use it combined with a find operation so that all my uses of the @Version with the Svn: $id keyword are deleted (they aren't really needed after a git migration)

find . -type f -name "*.class.php" -exec ~/line_cleaner.pl " @Version " {} ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment