|
|
split text file?
hi everyone,
I have a report that has three headings throughout the report. I want
to split the report into three text files called section 1, section 2,
section 3. is there a simple way of accomplishing this in perl?
tia
eddiec :-)
|
|
0
|
|
|
|
Reply
|
chalk (34)
|
1/12/2006 5:48:21 AM |
|
pc wrote:
> I want to split the report into three text files called section 1, section 2,
> section 3. is there a simple way of accomplishing this in perl?
Sure, piece-o-cake, especially with a little help from IO::All. This
assumes, of course, that your "heading" is distinctive and won't occur
within the main text. You didn't provide a sample of your headings or
data, so I made something up based on comic-strip characters, which I
read from __DATA__ for usenet convenience; adapt per your requirements:
#!/usr/bin/perl
use strict; use warnings;
use IO::All;
my $io; # [ IO::All handle ]
while ( <DATA> ) {
$io = io("/tmp/$1.txt") if /== (.*) ==/;
$io -> print($_) if $io;
}
__DATA__
== PEANUTS ==
Charlie Brown
Lucy
== DILBERT ==
Dilbert
Wally
Pointy-Haired Boss
== GARFIELD ==
Odie
Jon
--
http://DavidFilmer.com
|
|
0
|
|
|
|
Reply
|
usenet
|
1/12/2006 7:36:53 AM
|
|
pc <chalk@netspace.net.au> wrote:
> I have a report that has three headings throughout the report. I want
> to split the report into three text files called section 1, section 2,
> section 3. is there a simple way of accomplishing this in perl?
Yes.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
0
|
|
|
|
Reply
|
Tad
|
1/12/2006 12:00:48 PM
|
|
you can do it on the command line:
perl -ne 'if(/=heading pattern=/) {$n++;open FH,">","section $n"}
print FH;END{close FH}' file.txt
or if you don't need the heading into your splited files:
perl -ne 'if(/=heading pattern=/) {$n++;open FH,">","section
$n";next} print FH;END{close FH}' file.txt
At least for some small-sized files, the "END{close FH}" statement can
often be neglected.
Good luck,
Xicheng
pc wrote:
> hi everyone,
>
> I have a report that has three headings throughout the report. I want
> to split the report into three text files called section 1, section 2,
> section 3. is there a simple way of accomplishing this in perl?
>
> tia
>
> eddiec :-)
|
|
0
|
|
|
|
Reply
|
Xicheng
|
1/12/2006 3:36:33 PM
|
|
|
3 Replies
60 Views
(page loaded in 0.107 seconds)
|
|
|
|
|
|
|
|
|