last entry in a file - Perl

This is a discussion on last entry in a file - Perl ; Is there a quick and easy way in perl to get the last entry in a file for a specific value For example If I have the following data in a file <len>1<\len> <type>int<\type> <len>2<\len> <type>int<\type> <len>3<\len> <type>int<\type> I would ...

+ Reply to Thread
Results 1 to 3 of 3

last entry in a file

  1. Default last entry in a file

    Is there a quick and easy way in perl to get the last entry in a file
    for a specific value

    For example If I have the following data in a file

    <len>1<\len>
    <type>int<\type>
    <len>2<\len>
    <type>int<\type>
    <len>3<\len>
    <type>int<\type>

    I would like to get the line <len>3<\len>.

    Please keep in mind that the data in the file is only a sample and it
    will not alwasy be the second line from the bottome :-)

    I know I can read the file and keep track via a flag of where I am in
    the file but this seems way to over-kill. I was hoping for some nice
    easy grep like command.

    Thanks in advance


  2. Default Re: last entry in a file

    paul_0403@yahoo.com writes:

    > Please keep in mind that the data in the file is only a sample and it
    > will not alwasy be the second line from the bottome :-)
    >
    > I know I can read the file and keep track via a flag of where I am in
    > the file but this seems way to over-kill. I was hoping for some nice
    > easy grep like command.


    If its the data in the len lines then you can just find it with a
    regex and the last find will be the one you get.

    while (<FILE>){
    if (/regex/){
    $var = $_;
    }
    }
    print $var . "\n";

    The value of $var will change at each hit, but if you only want the
    last one then thats what will be in var at the print


  3. Default Re: last entry in a file

    >>>>> "paul" == paul 0403 <paul_0403@yahoo.com> writes:

    paul> Is there a quick and easy way in perl to get the last entry in a file
    paul> for a specific value

    paul> For example If I have the following data in a file

    paul> <len>1<\len>
    paul> <type>int<\type>
    paul> <len>2<\len>
    paul> <type>int<\type>
    paul> <len>3<\len>
    paul> <type>int<\type>

    paul> I would like to get the line <len>3<\len>.

    paul> Please keep in mind that the data in the file is only a sample and it
    paul> will not alwasy be the second line from the bottome :-)

    You haven't stated the problem sufficiently. How come it isn't

    <type>int<\type>

    What does "last entry for a specific value" mean? To me, that's
    the last entry in the file. What does "specific" mean, specifically?

    By the way, is this some bizarro mirror XML, or do you really mean
    <type>int</type>
    ?

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

+ Reply to Thread