Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-15
8
Medium Priority
?
364 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 27

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

 

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 27

Accepted Solution

by:
wilcoxon earned 2000 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

971 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