Skip to content

Instantly share code, notes, and snippets.

@kontrafiktion
Created February 5, 2014 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kontrafiktion/8822015 to your computer and use it in GitHub Desktop.
Save kontrafiktion/8822015 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
# Replace everything between two two regexes (inclusive).
# The whole file is considered, so the matching block can span multiple lines
# The multi-line matching syntax of sed is (for me) total gibberish
# and having a replacement with newlines was just PITA
# therefore this simple script to replace everything bewteen two
# regexes by a (possibly) multi-line replacement
if ( @ARGV != 4 ) {
print "usage: $0 <start-regex> <end-regex> <replacement> <filename>\n";
exit 1
}
my $START=shift;
my $END=shift;
my $REPLACEMENT=shift;
my $filename=shift;
local $/ = undef;
open INFILE, $filename or die "Could not open file. $!";
my $string = <INFILE>;
close INFILE;
$string =~ s/$START.*?$END/$REPLACEMENT/sm;
print $string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment