Perl File open and read in reverse

Posted on 2006-11-01
Last Modified: 2008-03-17
Here is what I have...

open (FILE1,"../gt/$in{'id'}/rafs.txt") || "Can't open filename1 $!";
while( <FILE1> ){

How can I make it open and read from bottom up?  I know reverse command, but not sure where to stick it in at.
Question by:precision
LVL 17

Accepted Solution

mjcoyne earned 500 total points
ID: 17850074
#!/usr/bin/perl -w
use strict;

open (FILE1,"../gt/$in{'id'}/rafs.txt") or die "Can't open rafs.txt $!";

my @lines = <FILE1>;
@lines = reverse @lines;

for (@lines) {
  my($count,$wa,$id,$n1,$e1,$n2,$e2,$n3,$e3,$n4,$e4) = split/\|/;
LVL 43

Expert Comment

ID: 17850551
This will work for small files that fits into available memory. Otherwise smarter code is needed. The simplest way is to use existing sollutions

open(F, "tac 'filename' |") || die;
LVL 84

Expert Comment

ID: 17853906
  use File::ReadBackwards ;

    tie *FILE1, 'File::ReadBackwards', "../gt/$in{'id'}/rafs.txt" or die "can't read '../gt/$in{'id'}/rafs.txt' $!" ;

    while( <FILE1> ) {

Author Comment

ID: 17857239

That seems like a very efficient way, I tried though and it crashed, I tried a few things but could not make run...
LVL 84

Expert Comment

ID: 17861238
What did you try and how did it crash?
Do you have File::ReadBackwards installed?

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can Oracle Client work with an Oracle 12c Database? 5 143
Move Function in Perl Script 2 65
Perl Awk Need Help 3 109
what are these modules in perl script 4 93
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 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

11 Experts available now in Live!

Get 1:1 Help Now