Whats the proper way to get last line of a file...
Discuss Whats the proper way to get last line of a file... in the Perl Programming forum on Dev Shed. Whats the proper way to get last line of a file... Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 12,668
Time spent in forums: 4 Months 2 Weeks 3 h 7 m 53 sec
Reputation Power: 6120
using seek, you could pull in a big chunk of data into an array, reverse it, join it into a string, use index to find the location of the first "\n", substr to extract the last line in reverse, split on '' into an array, then reverse and join again ...
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones
09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
Posts: 1,795
Time spent in forums: 2 Weeks 5 Days 15 h 23 m 11 sec
Reputation Power: 82
Quote:
Originally Posted by papajohns
a simple
$variable = `tail -f file`;
...
or a system call...
`tail -f file` is a system call. backticks are the system operator. It is just as if you were ssh'ed into the sytem and you typed that at the command line. You are calling the executable tail and feeding it some command line parameters.
So we are down to 2 methods:
a system call (such as tail)
or reading the file manually, your choice.
Posts: 12,668
Time spent in forums: 4 Months 2 Weeks 3 h 7 m 53 sec
Reputation Power: 6120
Technically the seek option isn't the same since you can get the length of the file from the stat function and set the file pointer accordingly. Tie::File, I *assume* requires reading the file into an array, because how you can treat the file when Tied, File::ReadBackwards, I'm not sure about, haven't looked at the code.
Posts: 454
Time spent in forums: 2 Days 13 h 23 m 58 sec
Reputation Power: 12
mm..but from the example there it seems seek needs a number of bytes back from the end of the file...
anyway you can use seek, but then only get it up to the last carriage return so you only get the last line as oppose to a certain of bytes and then have to regex it somehow?
Posts: 12,668
Time spent in forums: 4 Months 2 Weeks 3 h 7 m 53 sec
Reputation Power: 6120
Do we know the record length, or any idea of the max possible record length?
set the pointer to file size - max rec length*2 (to be sure)
read for max rec length * 2
split the buffer on \n into an array
and the last populated item in the array is your last line
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 8,978
Time spent in forums: 1 Month 2 Weeks 6 Days 22 h 11 m 29 sec
Reputation Power: 3561
The proper PORTABLE way would be to read the file manually. You can either start from the beginning and work your way through all the lines, but the faster way would be to seek to the end and read chunks of the file and use rindex() to find \n.
Any solutions that use the tail command are assuming you're running on a unix or unix-based system (or have cygwin installed). Hence, they are potentially unportable.
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne Diary of a first time dog owner <-- my cousin's blog
Posts: 1
Time spent in forums: 5 m 14 sec
Reputation Power: 0
After seek to the end of file, it rewind at 1 byte steps until find the begin ("\n") of the last line.
my $pos=-1;
my $char;
my $already_nonblank = 0;
while (seek (LOGFILE,$pos--,2))
{
read LOGFILE,$char,1;
last if ($char eq "\n" and $already_nonblank == 1);
$already_nonblank = 1 if ($char ne "\n");
}
my $last_line = <LOGFILE>;