#!/usr/athena/bin/perl # process-comments.pl # Aneel Nazareth (achmed@mit.edu) # for usage type process-comments.pl -h # for documentation see http://web.mit.edu/cwis/faq/process-comments.html # comments to cwis-help@mit.edu # last updated 01/08/2001 # by Matthew Seegmiller (xaco@mit.edu) $begin_record = '{begin-record}'; $field_separator = '{field-separator}'; $end_record = '{end-record}'; $separator = "\t"; $return_conversion = ' '; $return_cvt = 1; $separator_conversion = ' '; $separator_cvt = 1; $append = 1; $verbose = 1; $lineno = 1; $linenos = 0; $outfile = 'process.out'; $switches=''; while (@ARGV) { $cmdlinearg = shift @ARGV; if ($cmdlinearg =~ /^([-+])([^ ]*)/) { if (($2 eq 'h') || ($2 eq 'help')) { die "For documentation see http://web.mit.edu/cwis/faq/process-comments.html\n"; } # -o controls outfile $outfile = shift @ARGV if ($2 eq 'o'); # print "outfile: $outfile\n"; # +/-a controls appending $append = ($1 eq '+') if ($2 eq 'a'); # +/-v controls verbosity $verbose = ($1 eq '+') if ($2 eq 'v'); # +/-r controls return conversion $return_conversion = shift @ARGV if ($2 eq 'r') && ($1 eq '+'); $return_cvt = 0 if ($2 eq 'r') && ($1 eq '-'); # +/-t controls separator conversion $separator_conversion = shift @ARGV if ($2 eq 't') && ($1 eq '+'); $separator_cvt = 0 if ($2 eq 't') && ($1 eq '-'); # -s controls separator $separator = shift @ARGV if ($2 eq 's'); # +/-n controls line-numbering $linenos = ($1 eq '+') if ($2 eq 'n'); # -br -fs -er control separators $begin_record = shift @ARGV if ($2 eq 'br'); $field_separator = shift @ARGV if ($2 eq 'fs'); $end_record = shift @ARGV if ($2 eq 'er'); } else { push(@infiles,$cmdlinearg); } } if ($append) { open(OUTFILE,">>$outfile") || die "Can't open target file $outfile: $!, stopped at"; warn "Appending to file '$outfile'\n"; if ($linenos) { open(LINENO,"wc -l $outfile |"); $_=; $lineno=1+$& if m/\d+/; close(LINENO); warn "Starting with record number $lineno\n"; } } else { if (-e $outfile) { print STDOUT "File $outfile exists. Replace? "; $_=; exit if /n/; } open(OUTFILE,">$outfile") || die "Can't open target file $outfile: $!, stopped at"; } select OUTFILE; foreach $infile (@infiles) { open(INFILE,"<$infile") || die "Could not open $infile: $!, stopped at"; print STDOUT " Processing $infile...\n" if $verbose; while() { if (s/^$begin_record(\n)?//) { print STDOUT " record " if $verbose; print $lineno++,"\t" if $linenos; while(defined $_) { if (s/$end_record(\n)?//) { print "\n"; print STDOUT "\n" if $verbose; last; } s/$separator/$separator_conversion/gi if $separator_cvt; print STDOUT "field " if (s/$field_separator(\n)?/$separator/gi) && $verbose; s/\n/$return_conversion/ if $return_cvt; print; $_ = ; } } } close(INFILE); print STDOUT " done.\n" if $verbose; } close(OUTFILE); print STDOUT "Finished.\n" if $verbose;