As for the regex part, I'm not sure since I'm not familiar with using the combo of regexes and the range operator. However, I'd advise going a different route than using the substr command to find the critical data that your trying to key in on. It looks like you're data is somewhat columnar, which means that using split /\s+/ can be used to create an array with each column stored in a different element.
The code below is how I would approach the problem. It kind of looks like you're planning to add more code to do other stuff, which means that the code below would probably need to be modified to fit in with your bigger plan.
Code:
use strict;
use warnings;
my $infile = "data.txt";
my $outfile = "peptest.txt";
open(DATA,"<",$infile) || die "Unable to open file '$infile': $!\n";
my $file;
# Slurp in the file into memory
{
local $/;
$file = <DATA>;
}
close(DATA);
open(OUTPUT,">",$outfile) || die "Unable to open file '$outfile': $!\
+n";
my ($section) = ($file =~ m/(NAME.*)ADJ/is);
my (@lines) = split /\n/,$section;
my $i = 0;
foreach my $line (@lines) {
if ($line =~ m/1235114182/) {
my (@cols) = split /\s+/,$line;
if ($cols[12] eq "0.00") {
print OUTPUT "$lines[$i]\n$lines[$i+1]\n";
}
}
$i++;
}
close(OUTPUT);
Output file:
12351141821118 111809 23 001 71010 26 31.00 0.00
+ 0.00 0.00 CO-18 31.00 0.00
+ N347
12351141821118 111809 23 001 74150 26 199.00 0.00
+ 0.00 0.00 CO-18 199.00 0.00
+ N347
12351141821118 111809 23 001 72192 26 182.00 0.00
+ 0.00 0.00 CO-18 182.00 0.00
+ N347
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|