SatisfyMyStruggles has asked for the wisdom of the Perl Monks concerning the following question:
Give the code data file content of the data records, I'm unable to print the first line of each record. However, it does print the first line of the record as in: 1aaaaaaaaaaaaa Note: I'm sure there are many ways to do this using the split function, but in my case, I need a solution that follows the code question.... I think the problem is that after chomp the input record separator of __Data__, the first record gets process correctly but the first line of the other records is a blank line, and therefor, the output shows a blink line as the first line. I try using a input record separator of __Data__\n and did not chomp
Data file content of records 1aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa __Data__ 1bbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbb __Data__ 1cccccccccc ccccccccccc ccccccccccc __Data__
{ local $/="__Data__"; open my $fh,"<","logA.txt" or die "Unable to open file"; while(<$fh>) { # remove the input record separator value of __Data__ chomp; # display the line of each record if(/([^\n]*)\n(.*)/sm) { print "$1\n"; } } close ($fh); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to display first line of each record using the question code
by LanX (Saint) on May 18, 2013 at 17:28 UTC | |
by SatisfyMyStruggles (Initiate) on May 18, 2013 at 17:36 UTC | |
by LanX (Saint) on May 18, 2013 at 17:40 UTC | |
| |
|
Re: How to display first line of each record using the question code
by Laurent_R (Canon) on May 18, 2013 at 18:49 UTC | |
by Anonymous Monk on May 18, 2013 at 19:33 UTC | |
by Anonymous Monk on May 18, 2013 at 19:48 UTC | |
by SatisfyMyStruggles (Initiate) on Jun 05, 2013 at 23:14 UTC |