On Jun 4, Pedro A Reche Gallardo said:
>perl -ne 'BEGIN{$/=">"}if(/^\s*(\S+)/){open(F,">$1")|| warn"$1 write
>failed:$!\n";chomp;print F ">", $_}'
>This command will take a file like this:
>name1: anything
kdkdkkdkk
dkdkkdkdk
>name2: anything
slkslsksksl
>and convert it into two files, named as name1 and name2, and containing
>the following information
Well, I first suggest running the program through the Perl Deparser:
japhy% perl -MO=Deparse -ne 'BEGIN{$/=">"}..., $_}'
LINE: while (defined($_ = <ARGV>)) {
if (/^\s*(\S+)/) {
warn "$1 write failed:$!\n" unless open F, ">$1";
chomp $_;
print F '>', $_;
}
}
And that is almost all you need to do to get it to run in a
program. Granted, the BEGIN { } block dropped out (and it's rather
important), but you can put that back in.
{
local $/ = "\n>"; # I'd use "\n>" instead of just ">"
local @ARGV = $file_to_parse;
while (<>) {
if (/^\s*(\S+)/) {
open F, ">$1" or warn "$1 write failed: $!";
chomp;
print F ">$_";
}
}
}
The code basically reads a "line" as that which ends in a newline followed
by a ">". It extracts the first string of non-whitespace characters as
the name of the file to create, and then prints that "line" to the newly
created file.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **