in reply to Re^2: Need help to make correction in a perl script
in thread Need help to make correction in a perl script

Like so many things, the solution is easy to understand once you know it but difficult to see when you don't know where to look.

Your program reads the last two lines and copies them into your variable $line but it exits without ever printing the contents of this variable. All you have to do is print the contents of this variable after reading the last line of the file.

Your loop terminates after reading the last line of the file. Therefore, to print the contents of $line after reading the last line of the file, you need to add a print statement after your loop.

Your program is otherwise a bit more complex than it needs to be. Here is a simplified version, with a final print added, to print that troublesome last line.

#!/usr/bin/perl -w use strict; use warnings; my $line; while (<DATA>) { chomp; if (/^$/) { print "\n"; $line = ""; } elsif (/^[A-Z]{2}:\s/) { print "$line\n" if($line); $line = $_; } else { $line .= " $_"; } } print "$line\n" if($line); __DATA__ DN: Data Name Information Sciences TI: Title Distribution and abundance of bound volumes of the file in institute shelves from 1978 AU: Author Sahu, SR; Gawas, AKG AF: Affiliation Inst. of document scanning and documentation profiling at library, toward the prograssive society and establishment SO: Source Test document file. Vol. 5, no. 1, pp. 1-14. Jul 1990. DE: Descriptors Article Subject Terms: Abundance; Ecological distribution; Geographical distribution; Life cycle; Zooplankton; Article Taxonomic Terms: Euphausia; Article Geographic Terms: ISW, LA: Language English