#!/usr/bin/perl # The Missing Textutils, Ondrej Bojar, obo@cuni.cz # http://www.cuni.cz/~obo/textutils # # 'makeprefix' will scan for a given regular expression and use it as a prefix # for all the following lines until another match is found. Use () to mark a # subpart of the RE. # # Use --head for the RE default for parsing 'head *' output. # # $Id: makeprefix,v 1.4 2006/12/21 07:15:40 bojar Exp $ # use Getopt::Long; use strict; my $delim = "\t"; my $head = 0; my $ignoreblank = 0; my $usage = 0; my $keep = 0; GetOptions("head" => \$head, "delim=s" => \$delim, "ignore-blank" => \$ignoreblank, "help" => \$usage, "keep" => \$keep, ); my $re = shift; if ($head) { $re = "^==> (.*) <==\$"; $ignoreblank = 1; } if ($usage || !$re) { print STDERR "usage: makeprefix Options: --head ... Ignore the arg1 and use '^==> (.*) <==\$', useful for parsing the output of the 'head' command --ignore-blank ... ignore blank lines in input --delim=DELIMITER ... use this as the separator between the prefix line and the data lines. Default: TAB --keep ... don't delete the line where the prefix was found "; exit 1; } my $prefix = ""; while (<>) { next if $ignoreblank && /^\s*$/; if (/$re/o) { if (defined $1) { $prefix = $1; } else { $prefix = $_; } chomp $prefix; print $prefix.$delim.$_ if $keep; } else { print $prefix.$delim.$_; } }