Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl, Reading last line from a file

Posted on 2004-04-30
4
Medium Priority
?
781 Views
Last Modified: 2010-04-21
Hi!

i got a text file with multiple lines, and want to save a field from the last line of this file into a variable... how i can do it?

Apr 30 13:48:46 mobydic pop3d: LOGIN, user=z100332-m429, ip=[::ffff:80.33.236.71]
Apr 30 13:48:47 mobydic pop3d: LOGOUT, user=z100332-m429, ip=[::ffff:80.33.236.71], top=0, retr=0
Apr 30 13:51:00 mobydic pop3d: LOGIN, user=z100332-m429, ip=[::ffff:80.33.236.71]
Apr 30 13:51:00 mobydic pop3d: LOGOUT, user=z100332-m429, ip=[::ffff:80.33.236.71], top=0, retr=0
Apr 30 13:54:02 mobydic pop3d: LOGIN, user=z100332-m429, ip=[::ffff:80.33.236.71]
Apr 30 13:54:02 mobydic pop3d: LOGOUT, user=z100332-m429, ip=[::ffff:80.33.236.71], top=0, retr=0
Apr 30 13:54:59 mobydic pop3d: LOGIN, user=z100332-m429, ip=[::ffff:80.33.236.71]
Apr 30 13:54:59 mobydic pop3d: LOGOUT, user=z100332-m429, ip=[::ffff:80.33.236.71], top=0, retr=0

Want to save into $date ---> Apr 30 13:54:59

Thanks in advance!
0
Comment
Question by:morsa801tons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:fim32
ID: 10961140
i suppose you could do this in a couple of different ways.  the easiest would probably be to grab the last line with last, and cut the value you want:

$line = `last -1 textfile`;
$line =~ /(.{15})/;
$date = $1;

if you don't have last, or want to do an all-perl solution... well, depending on how long this text file is, you could just suck and throw away every line:

open FH,"textfile";
while (! eof(FH)) { $line=<FH>; }
$line=~/(.{15});
$date=$1;
0
 
LVL 6

Expert Comment

by:bira
ID: 10962699
a=`tail -1 textfile`
echo $a
0
 
LVL 6

Accepted Solution

by:
bira earned 375 total points
ID: 10962730
a=`tail -1 file|cut -f1 -d'm'`
echo $a
0
 
LVL 84

Expert Comment

by:ozo
ID: 10964233
#if you have Perl 5.8
use Tie::File;
tie @array, 'Tie::File', "textfile" or die $!;
$date=substr($array[-1],0,15);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question