Solved

Perl, Reading last line from a file

Posted on 2004-04-30
4
760 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
  • 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 125 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now