mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks
We will be downloading files from server...we have to check for end of
file starts with "T" if this is there then we treat
this as complete file and do further processing otherwise
its not a complete file and we will
wait for the file. for eg. if the file has the following
0000000508120060901 3TEST C TEST 0000000508220060901 + TEST123 0000000005120060901 + 300055566 0000000509720060901 + 1ABC30620060901 000DDDD 24702R101 B000000272730 +000000000500000000 OTC + C + 2TC12345 00000000188 JEMPRUTEST 000000089 +89 TONY C TEST 00000008990 TEST123 0000 +0000090 200055566 00000009016 + TJHY30600000000009
As above the last line started with "T" we can assume its complete file..


Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: Finding a Letter at Last line
by davidrw (Prior) on Nov 08, 2006 at 16:17 UTC
    TWTOWTDI :)
    perl -lne '$s=$_; END{ print "OK" if $s=~/^T/ }' /tmp/foo
    Though you might want exit codes instead, depending on what's calling this
    perl -ne '$s=$_; END{ exit($s=~/^T/ ? 0 : 1) }' /tmp/foo
    Or, using File::Tie:
    perl -MTie::File -e 'tie @f, "Tie::File", shift; exit 1 unless $f[-1] +=~ /^T/' /tmp/foo
    Or, using File::ReadBackwards (untested):
    perl -MFile::ReadBackwards -e 'exit 1 unless File::ReadBackwards->new( +shift)->readline =~ /^T/' /tmp/foo
Re: Finding a Letter at Last line
by smokemachine (Hermit) on Nov 08, 2006 at 15:58 UTC
    could be this?
    perl -e 'open FILE, "<file"; $_ = join "", <FILE>; print "Download fin +ished" if /^t[^\n]*\Z/msi'
      Hey smokemachine
      Thanks for the reply..it worked..just a small doubt how can i replace "<file" with variable filenames??
      Thanks & Regards
      Sridhar
        yes... you can use "<$filename"
Re: Finding a Letter at Last line
by davido (Cardinal) on Nov 08, 2006 at 17:07 UTC

    You might like using File::ReadBackwards, since it efficiently begins reading chunks from the end of the file, without wasting time starting at the beginning and paging your way through the file until you find the end.

    my $bw = File::ReadBackwards->new( 'your.filename' ) or die "can't read 'your.filename'\n$!"; if( defined( my $last_line = $bw->readline ) and $last_line =~ m/^T/ ) { # do your processing here... }

    If it turns out that it's ok to have leading space before the 'T', you would have to modify the regexp to look like this: m/^\s*T/

    Another convenient way (though less efficient) is Tie::File, where your file becomes an array. The last line could be accessed as $file[-1].


    Dave

Re: Finding a Letter at Last line
by johngg (Canon) on Nov 08, 2006 at 16:09 UTC
    But the last line doesn't start with a "T". Do you mean the last field of the last line?

    Cheers,

    JohnGG

Re: Finding a Letter at Last line
by fenLisesi (Priest) on Nov 08, 2006 at 16:08 UTC
    How big can these files get? (btw, the last line doesn't seem to start with T there)
Re: Finding a Letter at Last line
by Anonymous Monk on Nov 08, 2006 at 15:58 UTC
    Assuming you have the entire content of the file in a string called $var (if you have them line by line in an array, just see whether the first character of the last element is a T), you could do:
    $text =~ /(?:^|\n)T[^\n]*$/