Solved

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

Posted on 2011-03-15
8
319 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now