Please forgive my ignorance - I'm a bit rusty.
I am using regex to parse a data file. I am taking an exclusion approach (i.e., removing lines I don't want), but no matter what I do, the output contains blank lines for the lines excluded. I have tried chomp (which gets rid of all eol, which I don't want) and trimming (both commented out below), but to no avail. My exclusion criteria is working, but that may be part of the problem. Here's the code:
use strict;
use diagnostics;
use warnings;
# Define label variable
my $build = $ARGV[0];
die "\nScript aborted. Study code is missing as an argument.\n\nUsage:
+ $0 {STUDYCODE}\n" if @ARGV == 0;
open (LABELS, 'labels.txt') || die "Can't open labels.txt \n$!\n";
while (<LABELS>)
{
my $data_line = $_;
#
if ($data_line =~ /$build/)
{
# Remove lines beginning with "bl", "BL" or "_" and releases w
+ith a label with xx.yy.zzz.nnn(n)
$data_line =~ s/^[BbLl].*|^_.*|^.*\d\d\d\.\d\d\d.*//g;
#chomp ($data_line); #Remove end of lines
#$data_line =~ tr/\n//s; # Still have blank lines I need to re
+move
print "$data_line";
}
}
close LABELS;
exit;
Here is the input date I have in the file labels.txt:
_STUDYABCD1234_1.00
_STUDYABCD1234_1.00.5678
STUDYABCD1234_1.00
STUDYABCD1234_1.00.000
p_STUDYABCD1234_1.00.000
p_STUDYABCD1234_1.00.000.5678
bl_STUDYABCD1234_1.00.000
bl_STUDYABCD1234_1.00.000.5678
BL_STUDYABCD1234_1.00.000
BL_STUDYABCD1234_1.00.000.5678
There are ten lines in the input file, and the output also contains 10 lines, three of which show properly, and 7 blank lines that are the excluded lines. Any help is appreciated. Thanks.