Solved

How to create data records out of data records using Perl?

Posted on 2011-03-15
8
352 Views
Last Modified: 2012-05-11
I have the following data record below that starts with __Data__. Within this data record, I would like to create another data record using the __Data__ as the field separator.

Here's an example data record

__Data__
@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj
test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds

I would like the above output to be...

__Data__
@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj

__Data__
@test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds


Start code
-------------
#!/usr/bin/perl

use warnings;
use strict;

$/='__Data__';


    open 'FH','<',"/home/data_records";
    while(<FH>)
   {
       if(/(\@[^\n]*)(.*)(test_scsi[^\n]*)(.*)(test_scsi[^\n]*)(.*)/ms)
       {
         print $1,$2,$3,$4,"\n","__Data__","\@",$5,$6;
       }
    }

The above is far from working but its a limitation of my idea. But if there is a better way to do it, please let me know.
0
Comment
Question by:areyouready344
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:Fairlight2cx
ID: 35145931
#!/usr/bin/perl -w

use strict;

my $count = 0;
open(FH,"<data");
while (my $line = <FH>) {
     chomp($line);
     if ($line =~ /^\@test_scsi/) {
          print("\n__Data__\n") unless $line =~ /^__Data__\n$/ or $count == 0;
          $count++;
     }
     print("$line\n");
}
exit;
0
 

Author Comment

by:areyouready344
ID: 35147361
Thanks but did not work, the output came out like this....

@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj
test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds


It should come out like this:

__Data__
@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj

__Data__
@test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35148277
This will do it...
#!/usr/bin/perl

use strict;
use warnings;

$/='__Data__';

open 'FH','<',"/home/data_records";
while(<FH>) {
    print split_block($_);
}

sub split_block {
    my ($txt) = @_;
    return $txt unless ($txt =~ m{^(\@test_scsi.*?\n)(test_scsi.*)$}ms);
    return "$1\n__Data__\n" . split_block('@'.$2);
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:areyouready344
ID: 35149515
Once again, thanks Wilcoxon.

One question, how can I make the (test_scsi.*) more flexible for additional OR options.
For example, I tried the following but giving the same results.

((test_scsi|test_sas|test_sata).*)

total code...

#!/usr/bin/perl

use strict;
use warnings;

$/='__Data__';

open 'FH','<',"/home/data_records";
while(<FH>) {
    print split_block($_);
}

sub split_block {
    my ($txt) = @_;
    return $txt unless ($txt =~ m{^(\@test_scsi.*?\n)((test_scsi|test_sas|test_sata).*)$}ms);
    return "$1\n__Data__\n" . split_block('@'.$2);
}
0
 

Author Comment

by:areyouready344
ID: 35149599
Oops, For example, I tried the following but giving the same results.
Correct, For example, I tried the following but not giving me the same results as the original code.
0
 

Author Comment

by:areyouready344
ID: 35149846
One minor problem is spotted, first data record line does not line up correctly as in -->(__Data__@test_scsi_1) should be

__Data__
@test_scsi_1

------------------------------------------------------------------------------------
__Data__@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj

__Data__
@test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35149979
This should do it.  You can just change the $anchor definition to alter what text is considered the start of a __Data__ section.  This also fixes the first line issue.
#!/usr/bin/perl

use strict;
use warnings;

$/='__Data__';

my $anchor = '(?:test_scsi|test_sas|test_sata)';

open 'FH','<',"/home/data_records";
while (<FH>) {
    print split_block($_), "\n";
}

sub split_block {
    my ($txt) = @_;
    return $txt unless ($txt =~ m{^(\@$anchor.*?\n)($anchor.*)$}ms);
    return "$1\n__Data__\n" . split_block('@'.$2);
}

Open in new window

0
 

Author Comment

by:areyouready344
ID: 35150189
Everything is working correctly, thanks. But I made a mistake on the request. I'll close this ticket out and open another one.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 191
compress files in RAR using perl 13 94
Edit Odbc.ini using perl module 2 96
Perl Frameworks 1 119
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question