in reply to (jeffa) Re: when reading a file how to print a line one above the current line??
in thread when reading a file how to print a line one above the current line??

Actually, there are situations where one would like to read file names from STDIN -- e.g.:
ls *.log | show-the-alarms
in which case, it'll be handy to have the file name(s) included in the output (I haven't tested this):
while ( $file = <>) { chomp $file; my $status = "open file"; open( MYFILE, $file ) or next; $status = 0; my $last = ""; while (<MYFILE>) { if(/ALARM:/) { print "$file:", $. - 1, ": $last"; print "$file:$.: $_"; $status++; } $last = $_; } close MYFILE; } continue { print "Done with $file : found $status error(s)\n"; }
That might not be what the questioner had in mind, but it's a reasonable approach.

Replies are listed 'Best First'.
Re^3: when reading a file how to print a line one above the current line??
by Aristotle (Chancellor) on Jun 04, 2002 at 11:50 UTC
    You still shouldn't read the filenames from STDIN. What you mention can and should be done by the shell: show-the-alarms `ls *.log`. Adding this ill-advised "functionality" to the script will only make it unnecessarily difficult to parse data not coming from a file.

    Makeshifts last the longest.