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.


In reply to Remove blank lines from REGEX output by Deep_Plaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.