how to split this file?

H

Huajian Luo

Hi there,

I need to split the following file to 3 parts that's
==>1) Found regular diskset containing disks: to first empty line
==>2) Found regular diskset containing disks: to first empty line
.......
the "Found regular diskset containing disks" maybe 1 or more than 1.
==>the Warning: message, the rest of the file

Any hints on this?


__DATA__



1) Found regular diskset containing disks:
c1t10d0
c1t11d0

Creation time: Mon Nov 14 20:32:17 2005
For more information about this diskset:
metaimport -r -v c1t10d0
To import this diskset:
metaimport -s <newsetname> c1t10d0


2) Found regular diskset containing disks:
c1t12d0

Creation time: Mon Nov 14 20:33:10 2005
For more information about this diskset:
metaimport -r -v c1t12d0
To import this diskset:
metaimport -s <newsetname> c1t12d0



Warning: The following disks have been detected in more than one set.
Import recommendation based upon set creation time.
Proceed with the import with caution.
c1t11d0 - recommend importing with set created at : Mon Nov 14 22:06:48 2005


---end __DATA__
 
H

Huajian Luo

Purl Gurl said:
Yes, your "the rest of the file" is gibberish. Are readers to guess
at what "the rest of the file" contains? Why do you assume readers
to be psychic internet mind readers?

A hint is, "Write articles which are clear, concise and coherent."
I mean to split the __DATA__ to 3 parts
__DATA__


1) Found regular diskset containing disks:
c1t10d0
c1t11d0

Creation time: Mon Nov 14 20:32:17 2005
For more information about this diskset:
metaimport -r -v c1t10d0
To import this diskset:
metaimport -s <newsetname> c1t10d0

This is Part 1
2) Found regular diskset containing disks:
c1t12d0

Creation time: Mon Nov 14 20:33:10 2005
For more information about this diskset:
metaimport -r -v c1t12d0
To import this diskset:
metaimport -s <newsetname> c1t12d0 This is the Part 2



Warning: The following disks have been detected in more than one set.
Import recommendation based upon set creation time.
Proceed with the import with caution.
c1t11d0 - recommend importing with set created at : Mon Nov 14 22:06:48 2005


And this is the rest of the file.
 
U

usenet

Huajian said:
==>1) Found regular diskset containing disks: to first empty line
==>2) Found regular diskset containing disks: to first empty line
......
the "Found regular diskset containing disks" maybe 1 or more than 1.
==>the Warning: message, the rest of the file

This is a dreadful question (meaning it is very hard to ascertain your
intent). The best way to get a good answer is to ask a good question.
You have asked a very bad question, so you can only hope to get a very
bad answer (as I believe PG has already provided).

But, unlike PG, I am here to help you, not berate you. First of all,
you should read the posting guidelines for this group. They can be
found on-line at:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

These guidelines exist for YOUR benefit (because they show you how to
compose effective posts which are much more likely to get effective
responses - without getting flamed).
From what I was able to cut through the fog of the post, I believe
something like this would work (although the criteria for the split is
not robust - it can be easily broken by slight or unexpected changes in
input format):

#!/usr/bin/perl
use warnings; use strict;

my %section = (); #a hash of arrays, one array for each section
my $part = 0; #which part is being seen now

while (<DATA>) {
$part++ if /^\d|^Warning/;
push @{$section{$part}}, $_;
}

#verify the results
for (1..3) {
print "\n\n### THIS IS SECTION $_ ###\n",
@{$section{$_}},
"### End of Section $_ ###\n";
}


__DATA__
1) Found regular diskset containing disks:
c1t10d0
c1t11d0

Creation time: Mon Nov 14 20:32:17 2005
For more information about this diskset:
metaimport -r -v c1t10d0
To import this diskset:
metaimport -s <newsetname> c1t10d0

2) Found regular diskset containing disks:
c1t12d0

Creation time: Mon Nov 14 20:33:10 2005
For more information about this diskset:
metaimport -r -v c1t12d0
To import this diskset:
metaimport -s <newsetname> c1t12d0

Warning: The following disks have been detected in more than one set.
Import recommendation based upon set creation time.
Proceed with the import with caution.
c1t11d0 - recommend importing with set created at : Mon Nov 14
22:06:48 2005
 
H

Huajian Luo

But, unlike PG, I am here to help you, not berate you. First of all,
you should read the posting guidelines for this group. They can be
found on-line at:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

These guidelines exist for YOUR benefit (because they show you how to
compose effective posts which are much more likely to get effective
responses - without getting flamed).

Thanks and I'll check out it once I'm available.
#!/usr/bin/perl
use warnings; use strict;

my %section = (); #a hash of arrays, one array for each section
my $part = 0; #which part is being seen now

while (<DATA>) {
$part++ if /^\d|^Warning/;
push @{$section{$part}}, $_;
}
Yes, That's what I want, Thank your very much.
 
X

xicheng

Hi, Huajian:
if you want to split your data into separated files, you can try the
following code:
----
#!/usr/bin/perl -w
use strict;
my $file=1;
while(<DATA>) {
if( /^\d\)\s+Found regular diskset containing disks/ ){
open FH,">",sprintf("file_%03d.dat",$file++) or die "can't
write, $!";
select FH;
} elsif ( /Warning:/) {
open FH,"> warning.dat" or die "can't write Warning.dat, $!";
select FH;
}
print;
}
close FH;
 
U

usenet

#!/usr/bin/perl -w .... snip
while(<DATA>) {
if( /^\d\)\s+Found regular diskset containing disks/ ){
open FH,">",sprintf("file_%03d.dat",$file++) or die "can't write, $!"; .... snip
}
close FH;

Technically you only close the last file you opened. Perl should "clean
up" on exit and close open files, but most hacks would agree that it's
not good practice to do it this way.
 
X

xicheng

As I remember, in Perl, one can either explicitly close a filehandle by
close(FH), or automatically close the filehandle by attaching the same
filehandle to another file, i.e. using open(). coz I used the same name
"FH" as the filehandle, so only one close() is necessary..
Best,
XC
 
U

usenet

As I remember, in Perl, one can either explicitly close a filehandle by
close(FH), or automatically close the filehandle by attaching the same
filehandle to another file...

Yes, that's true. I should have looked at the code more closely before
responding. Even so, my preference is to always do explicit file
closures, but that's probably just a personal hang-up from days coding
in other languages where explicit closure was essential (and failure to
do so was disasterous).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top