Solved

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

Posted on 2011-03-15
8
339 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
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

831 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