Solved

Perl, Reading last line from a file

Posted on 2004-04-30
4
771 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
auto mounter on solaris 1 79
Skill Development 15 201
Using gawk to read and manipulate multiple texts from a file 1 85
awk sed 8 132
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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…
Suggested Courses

738 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