boby has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: split of files
by GrandFather (Saint) on Jun 22, 2007 at 09:18 UTC | |
Let's suppose for a moment that you wanted to split the data at the lines containing "INPUT SEQUENCE=" and use the number following the = as part of the file name for the output file, then you could:
Prints:
which may or may not be anything at all like what you had in mind, but then you haven't actually told us that so guessing is all we can do. DWIM is Perl's answer to Gödel | [reply] [d/l] [select] |
Re: split of files
by shmem (Chancellor) on Jun 22, 2007 at 09:40 UTC | |
There Is More Than One Way To Do It - one bizarre way is
update - oh. You want bunches of 1000 records in separate files?
update - a bit of explanation: setting the input record separator $/ (see perlvar) to the token right after the file boundary lets the diamond operator <> (or readline) read up to and including that token as a single line into $_. With chomp we remove $/ from the end; it is added up front when outputting. The ++$file is a string increment; doing that we get the next file name (File00001, File00002, ... ). Since the first "line" (consisting of the record separator only) isn't interesting, we do a <> before the loop. Next line is then number 2 ($. - see perlvar), and $.-2 % 1000 (modulo 1000) is 0 at line 2, 1002, 2002, ... , so we (re-)open the output filehandle at that line count, which does an implicit close. See open. --shmem
| [reply] [d/l] [select] |
Re: split of files
by citromatik (Curate) on Jun 22, 2007 at 09:24 UTC | |
If I understand correctly, you have a file with a number of records, each one beginning with a line:
and ending with:
And you want to split the whole file in smaller files containing N or these records The easiest way I can imagine doing this is using Tie::File. Try this script:
This script interfaces the file as an array, but in the way that you want it to do: Each record in the array corresponds with one logical record in the file. Once done, it splits the array (i.e. the records in the file) N by N records and outputs them in sub-files For example, if you call the script "split_records.pl" you can invoke it with:
Outputs:
Of course, the files containing the records are created too Hope this helps! citromatik | [reply] [d/l] [select] |
Re: split of files
by andreas1234567 (Vicar) on Jun 22, 2007 at 07:30 UTC | |
It is not quite clear to me what you want to do. Try to explain your algorithm that decides what should go into each file. There is helpful advice here on how to How (Not) To Ask A Question.
-- print map{chr}unpack(q{A3}x24,q{074117115116032097110111116104101114032080101114108032104097099107101114}) | [reply] |
Re: split of files
by Moron (Curate) on Jun 22, 2007 at 09:53 UTC | |
This would create the 270 files with suffixes .aa thru .jj
__________________________________________________________________________________
^M Free your mind!
| [reply] [d/l] |