perl - Reading multi-line records -
given code data file content of each data records, i'm unable print first line of each record. however, print first line of first record in: 1aaaaaaaaaaaaa
special note: i'm sure there many ways this, in case, need solution follows code question....
data file content of records
1aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa __data__ 1bbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbb __data__ 1cccccccccc ccccccccccc ccccccccccc __data__ { local $/="__data__\n"; open $fh,"<","loga.txt" or die "unable open file"; while(<$fh>) { # remove input record separator value of __data__\n chomp; # display line of each record if(/([^\n]*)\n(.*)/sm) { print "$1\n"; } } close ($fh); }
i undesirable output of:
[root@yeti]# perl test2.pl 1aaaaaaaaaaaaa [root@yeti]#
i need output of first line of each record.....
1aaaaaaaaaaa 1bbbbbbbbbbb 1ccccccccccc
to debug, find state of program diverges expectations, backtracking until find cause.
checking program reveals loop entered once , $_
contains entire file. means perl didn't find line terminator ("__data__\n"
).
examining file finds spaces between __data__
, newline. remove trailing spaces file.
perl -i -ple's/\s+\z//' loga.txt
Comments
Post a Comment