Created
September 15, 2011 17:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 " {} ;