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

Discussion in 'Perl Misc' started by bernd, Jun 19, 2006.

  1. bernd

    bernd Guest

    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
     
    bernd, Jun 19, 2006
    #1
    1. Advertising

  2. bernd

    Ben Morrow Guest

    Quoth "bernd" <>:
    >
    > 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

    --
    Giles: It's very common for Indian spirits to change to animal form.
    Buffy: [...] and, 'Native American'. G: Sorry? B: We don't say 'Indian'.
    G: Oh, right, yes; always behind on the terms... yes, still trying not to refer
    to you lot as 'bloody colonials'. [Buffy, 'Pangs']
     
    Ben Morrow, Jun 19, 2006
    #2
    1. Advertising

  3. bernd

    Guest

    "bernd" <> writes:

    > 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

    --
    Joel
     
    , Jun 19, 2006
    #3
  4. bernd

    Guest

    "bernd" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 19, 2006
    #4
  5. bernd

    Guest

    wrote:
    > "bernd" <> wrote:
    > > 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?


    Er, "tail", not "last".

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 19, 2006
    #5
  6. bernd

    Mumia W. Guest

    bernd wrote:
    > [...]
    > 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."
     
    Mumia W., Jun 19, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    490
    Jack Klein
    Nov 2, 2004
  2. Stu

    Detecting a PIPE in a program

    Stu, Feb 2, 2005, in forum: C Programming
    Replies:
    9
    Views:
    369
    Richard Bos
    Feb 4, 2005
  3. Trond Valen
    Replies:
    5
    Views:
    400
    Niklas Norrthon
    Dec 7, 2005
  4. Replies:
    1
    Views:
    227
    Ben Morrow
    Jun 2, 2004
  5. lambelly

    perl pipe reading

    lambelly, Jun 20, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    87
    J. Gleixner
    Jun 21, 2005
Loading...

Share This Page