![]() |
how to split this file?
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 <huajian.luo@bsd.world> |
Re: how to split this file?
Purl Gurl wrote:
> [typical PG code] Is that code supposed to DO something? use Data::Dumper and put a print Dumper(\@Array); at the end of the program. |
Re: how to split this file?
"Purl Gurl" <purlgurl@purlgurl.net> 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 <huajian.luo@bsd.world> |
Re: how to split this file?
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 |
Re: how to split this file?
usenet@DavidFilmer.com 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 <huajian.luo@bsd.world> |
Re: how to split this file?
usenet@DavidFilmer.com <usenet@DavidFilmer.com> 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 tadmc@augustmail.com Perl programming Fort Worth, Texas |
Re: how to split this file?
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 |
Re: how to split this file?
xicheng@gmail.com 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. |
Re: how to split this file?
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 |
Re: how to split this file?
xicheng@gmail.com 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). |
| All times are GMT. The time now is 07:42 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.