Reading from pipe with perl -na -e and detecting last line

B

bernd

Hello netties,

I cat a file (actually grepping some particular lines) to "perl -na -e"
and have to determine when I have read the last line of the content of
the file (which is actually "filtered" by grep already, so that I
cannot give perl the whole file as a command line argument).

Does somebody know how I detect the last line piped to perl? eof()
obviously does not work.

Cheers


Bernd
 
B

Ben Morrow

Quoth "bernd said:
I cat a file (actually grepping some particular lines) to "perl -na -e"
and have to determine when I have read the last line of the content of
the file (which is actually "filtered" by grep already, so that I
cannot give perl the whole file as a command line argument).

Does somebody know how I detect the last line piped to perl? eof()
obviously does not work.

~% perl -le'print for qw/a b c/' | perl -na -le'print "eof" if eof; print'
a
b
eof
c

Ben
 
J

jgraber

bernd said:
I cat a file (actually grepping some particular lines) to "perl -na -e"
and have to determine when I have read the last line of the content of
the file (which is actually "filtered" by grep already, so that I
cannot give perl the whole file as a command line argument).

Does somebody know how I detect the last line piped to perl? eof()
obviously does not work.

You could make a one-liner file that contains an end of file text line
that will be filtered in by grep, (untested)

echo 'grepstring ENDOFFILE' > endoffile
grep grepstring file1 file2 endoffile | \
perl -na -e 'if (/ENDOFFILE/) { eof stuff} {non eof stuff}'

However, since perl is a better grepper than grep,
my prefered solution is to include
the functionality of grep in the perl one-liner.
To detect the end of each of multiple files, use if (eof)
as listed in perldoc -f eof, but note that the eof test
has to be the first thing in the loop.
The following one-liner (with escaped \n between lines)
was tested using the entire text
of this post as the contents of file ex1
Note that I did not reset the count between files inside the eof block.

perl -na -e 'if (eof) {print STDERR "** eof $ARGV has $count\n"} \
next unless /grepstring/; ++$count; ' ex1 ex1

** eof ex1 has 3
** eof ex1 has 6
 
X

xhoster

bernd said:
Hello netties,

I cat a file (actually grepping some particular lines) to "perl -na -e"
and have to determine when I have read the last line of the content of
the file (which is actually "filtered" by grep already, so that I
cannot give perl the whole file as a command line argument).

Why not do the grep in Perl? or just pass the stream through "last" before
going into Perl?
Does somebody know how I detect the last line piped to perl? eof()
obviously does not work.

That is not obvious to me.

This does what I thought it would:

cat foo.pl | perl -lne 'print eof()? "last line: $_":$_'

It prints out all of foo.pl, with "last line: " preprended to the last
line.


Xho
 
M

Mumia W.

bernd said:
[...]
Does somebody know how I detect the last line piped to perl? eof()
obviously does not work.
[...]

You don't have to. Just put the code to be executed at the end of input
inside an END block.

Try "perldoc perlrun."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top