#!/usr/bin/env perl # PURPOSE: distribute lines on input file(s) (or stdin) across multiple # output files, determined by evaling some expression ARG1 in the context of # the line having been read. # EXAMPLE: # put lines beginning with asdf or qwer in files name asdf.lines and qwer.lines respecively (ignoring other lines): # distlines '"$1.lines" if m/^(asdf|qwer)/' # which COULD be written as a perl one-liner as # perl -n -e 'next unless m/^(asdf|qwer)/; $path = "$1.lines"; open($o{$path}, "> $path") unless exists $o{$path}; select($o{$path}); print' # put gff into one files per # distlines '@F=split; @F[3] if $#F=9' use strict; use warnings; my $pathform = shift; my %o; # hash from paths to open filehandles my $path = ''; my $lastpath; while (<>) { $path = (eval $pathform) || $path; #die $path; if ($path) { if (! $lastpath || $path ne $lastpath) { $lastpath = $path; if (! exists $o{$path}) { print STDERR $path . "\n"; open($o{$path}, "> $path") || die "opening $path" ; } select($o{$path}) || die "selecting $path"; } } print $_ ; }