Visit Our UNIX and Linux User Community


Perl: Reading in reverse.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Reading in reverse.
# 1  
Old 10-22-2009
Perl: Reading in reverse.

Is there a means of reading files in reverse? I just want to be able to read a file from the beginning and once I read a particular line, I start reading lines backward from there.

Now, I could toss everything into a string array, but the file I'll be reading is roughly 100MB. I don't want 100MB stored in memory. Any ideas here?

Also, is there a way to immediately purge an Array of it's elements without looping and popping? I'm looking to be efficient here.

Last edited by mrwatkin; 10-22-2009 at 02:23 PM..
# 2  
Old 10-22-2009
Natively: don't think so, but a quick search on CPAN brought up File::ReadBackwards

As for your 2nd question: if by purging you mean emptying, just assign an empty list:
Code:
@array = ();

# 3  
Old 10-22-2009
Do you have tac?
Code:
$> cat infile
     Mr. Praline: I'm sorry, I have a cold. I wish to make a complaint!
     Owner: We're closin' for lunch.
     Mr. Praline: Never mind that, my lad. I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.
     Owner: Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?
     Mr. Praline: I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!
     Owner: No, no, 'e's uh,...he's resting.
$> tac infile
     Owner: No, no, 'e's uh,...he's resting.
     Mr. Praline: I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!
     Owner: Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?
     Mr. Praline: Never mind that, my lad. I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.
     Owner: We're closin' for lunch.
     Mr. Praline: I'm sorry, I have a cold. I wish to make a complaint!

# 4  
Old 10-22-2009
Maybe this article on reversing files will give you an idea:

v14, i06: Eight Ways to Reverse a File

Previous Thread | Next Thread
Test Your Knowledge in Computers #193
Difficulty: Easy
Python was selected the Programming Language of the Year in 2007, 2010, and 2018.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

for loop - reverse reading

All, Here is my for loop export CFGLIST="LIST1 LIST2 LIST3" for i in $CFGLIST do echo print $i done The output will be LIST1 LIST2 LIST3 But i want it display LIST3 LIST2 LIST1 (8 Replies)
Discussion started by: baluchen
8 Replies

3. UNIX for Dummies Questions & Answers

reading a file in Perl

If a form's action is the following Perl script how do I make it print the entire contents of the file on the screen? if(param()) { my $uploadedFile = param('file');#in the html page 'file' is the value of the name attribute of the input my $fh = upload($uploadedFile); ... (1 Reply)
Discussion started by: zerohour
1 Replies

4. Shell Programming and Scripting

Perl: Reading data from other file

Hi, I am writting some perl scripts for daily backup process. In which I want to pass some data/referance from another txt file. Text file contains only one column and multiple rows. I want to pass this data to variables of another perl script. e.g. Refdoc.txt file contains data as: perl1... (3 Replies)
Discussion started by: n.dba
3 Replies

5. Shell Programming and Scripting

Reading values in perl

Hi whats the easiest way to read data from a feed file? sample data in a file called data.txt: name = varun ip = '23.43.123.2' address = "asd, blah blah blah ..... @#!$%$#%" i want to use this data in a perl script. I thought of initially reading it line by line n then cutting... (7 Replies)
Discussion started by: VGR
7 Replies

6. Shell Programming and Scripting

Perl - Reading keyboard keystroke

Hello All, I wounder if any one know if perl have the ability to run script in the background which record each keyboard keystorke? If yes , how can I implement the part which reading the keyboard keystroke? Is there any moudle that handle it ? Thanks a head Alalush (1 Reply)
Discussion started by: Alalush
1 Replies

7. Shell Programming and Scripting

perl - reading from a file conditionally

Hi, I am new to perl. I want to read from a file on the basis of some conditions.. I want to define parameters in a configuration file in such a manner like... etc.. in my perl script, theer is a variable like this.. then i want to read values from first if block from the file... (1 Reply)
Discussion started by: shellwell
1 Replies

8. Shell Programming and Scripting

PerL Reverse the string.

Hi, I am very new to perl. My question: How i can reverse the given string using substr function but without using reverse function in perl? Anybody please help. thanks, -Lalit (3 Replies)
Discussion started by: email-lalit
3 Replies

9. Shell Programming and Scripting

perl not reading my variable

I'm trying to make changes in a file using the following bash script: #!/bin/bash MYHOME=`echo $HOME` README=$MYHOME"/environment" IAM=`whoami` CHANGEPATHLIST="TALOG TACONFIG TAINFO TAWORK TMPSPACE" for var in $CHANGEPATHLIST do perl -pi -e 's/sacuser1/$IAM/ if m/$var/' $README... (3 Replies)
Discussion started by: yoonixq4u
3 Replies

10. Shell Programming and Scripting

Perl Reading from File

is there a perl equivalent to sscanf? or something where I get the strings separated by spaces? (1 Reply)
Discussion started by: karyn1617
1 Replies

Featured Tech Videos