Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to split this file?

Reply
Thread Tools

how to split this file?

 
 
Huajian Luo
Guest
Posts: n/a
 
      11-15-2005
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__


--
Thanks,

Huajian Luo <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-15-2005
Purl Gurl wrote:
> [typical PG code]


Is that code supposed to DO something? use Data:umper and put a
print Dumper(\@Array);
at the end of the program.

 
Reply With Quote
 
 
 
 
Huajian Luo
Guest
Posts: n/a
 
      11-15-2005
"Purl Gurl" <(E-Mail Removed)> writes:
> Huajian Luo wrote:
>
> > 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

>
> 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.

--
Thanks,

Huajian Luo <(E-Mail Removed)>
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-15-2005
Huajian Luo wrote:
> ==>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/cl...uidelines.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

 
Reply With Quote
 
Huajian Luo
Guest
Posts: n/a
 
      11-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> 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/cl...uidelines.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.

--
Thanks,

Huajian Luo <(E-Mail Removed)>
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-15-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:
> Purl Gurl wrote:
>> [typical PG code]

>
> Is that code supposed to DO something?



Yes, it is supposed impress the poor folks that don't know any better.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
xicheng@gmail.com
Guest
Posts: n/a
 
      11-15-2005
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;
----
Good luck,
XC

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-15-2005
(E-Mail Removed) wrote:

> #!/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.

 
Reply With Quote
 
xicheng@gmail.com
Guest
Posts: n/a
 
      11-15-2005
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

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-15-2005
(E-Mail Removed) wrote:
> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments