Home > Archive > PERL Beginners > January 2008 > last entry in a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
last entry in a file
|
|
| Paul 0403 2008-01-17, 10:02 pm |
| 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
| |
| reader@newsguy.com 2008-01-17, 10:02 pm |
| 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
| |
| Randal L. Schwartz 2008-01-17, 10:02 pm |
| >>>>> "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!
|
|
|
|
|