I wrote the following simple perlPerl script to read lines from stdinstdin that are the output from a psqlpsql call that returns lines of the form key1 | key2 | long text field and create a separate output file for each line, whose name is key1_key2_NNN.txt, where NNN is just a counter to ensure unique file names.
my $count = 0;
while (<>) {
if (/(.*)\|(.*)\|(.*)/) {
$count++;
my $outname = "$1_$2_$count.txt";
my $text = $3;
$outname =~ s/\s+//g;
my $outfile = new IO::File("> $outname");
$outfile->print("$text");
}
}
This does the trick, but there are 2 things I'm not thrilled about
I'd like to be able to do the $count++
$count++inline while putting the incremented number into the string, but I'm not sure how to not just get "0++"0++plugged in (i.e. how to make it know that the ++++is a command and not part of the literal string).I don't like that I have to save $3
$3to another variable, but if I don't, it gets cleared out before I need it.
Anyhow, any suggestions on making this a bit more slick?