A simple Perl program to delete the part of a file that is matched by a regular expression, for every file in the "pages" directory:
#! /usr/bin/perl
# [[SnipPattern]] version 0.1 --- ^z = [[MarkZimmermann]] --- 3 Sep 2001
# delete part of a file, the part that matches a regexp
# To experiment with this, invoke:
# perl [[SnipPattern]].perl 'regexp'
# from right above a "pages" directory *(A COPY, NOT THE ORIGINAL!)*
# and then the first thing that matches the regexp will be deleted
# BEWARE OF RUNAWAY PATTERNS! REMEMBER GREEDINESS OF MATCHING!
# examples:
# to remove everything before the first comma, try: '^.*?,'
# to remove everything after the final "----", try: '----[^-]*^'
# to remove Bo's Wiki headers try: '^.*\263text\263'
# to remove Bo's Wiki footers try: '\263date\263.*$'
# to remove previous [[CorrelOracle]] note try:
# '\n----\n[^-]*If you liked this page, the [[CorrelOracle]].*$'
print "[[SnipPattern]] v.0.1 - BEWARE!\n";
opendir(DIR, "pages") or die "couldn't open 'pages'";
@pages = grep !/^\./, readdir DIR;
undef \\; # grab entire files at once
$snip = @ARGV[0];
foreach $page (@pages) {
if ( -e "pages/$page" ) {
open(F, "pages/$page") or die "$page: $!";
print " $page ... ";
$body = <F>;
close(F);
$body =~ s/$snip//so; # do it on whole body, only once
open(F, ">pages/$page") or die "$page: $!";
print F $body;
close(F);
} else {
die "$page didn't exist: $!";
}
print "\n";
}
(see SnipPattern)
TopicProgramming - 2001-09-06
(correlates: Convert to Creole, CorrelOracle01SourceCode, CorrelOracle02SourceCode, ...)