#! /local/bin/perl -s
#
# rsplit	--- split a file of records into two parts by a keyword
#
# Split a file of records into <file>.1 and <file>.2 respectively
# containing and not containing the <key> word.
# The output files may be given as arguments.
# If the mismatch file is missing, the source is assumed.
#
# Author: Oscar Nierstrasz (oscar@cuisun.unige.ch) 890516
#
# Jan 30/93 -- replaced sed version by awk version (faster, and more robust)
#		[sed silently chops lines longer than 4000 chars!]
# Jul 3/93 -- re-written in perl (even faster, and adds -i option)

$usg = "Usage: rsplit [-i] <key> <file> [<match-file> [<mismatch-file>]]\n";

# blank line is record separator
$/ = "";

# $IN is the input file
# $MATCH will contain the matching records
# $REST will contain the non-matching records

$key = $ARGV[0];
if ($#ARGV == 1) { $IN = $ARGV[1]; $MATCH = "$IN.1"; $REST = "$IN.2"; }
elsif ($#ARGV == 2) {
	# move input to a temporary file:
	$REST = $ARGV[1]; $MATCH = $ARGV[2]; $IN = $MATCH.$$;
	link($REST,$IN) || die "Can't link $REST to $IN\n";
	unlink($REST) || die "Can't mv $REST to $IN\n";
	$rm = 1;
	}
elsif ($#ARGV == 3) { $IN = $ARGV[1]; $MATCH = $ARGV[2]; $REST = $ARGV[3]; }
else { die $usg; }

open(IN,$IN);
open(MATCH,">$MATCH");
open(REST,">$REST");

if ($i) {
	while (<IN>) {
		/$key/io && do { print MATCH $_ ; next; };
		print REST $_;
	}
}
else {
	while (<IN>) {
		/$key/o && do { print MATCH $_ ; next; };
		print REST $_;
	}
}

if ($rm) { unlink($IN); }

# NB: should be able to avoid the code duplication by using "eval"
# with $i following the pattern match, but doesn't work ...

__END__

