Computing.Net > Forums > Programming > Parse file backwards?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Parse file backwards?

Reply to Message Icon

Name: marklunda
Date: October 12, 2005 at 02:56:16 Pacific
OS: Linux Fedora
CPU/Ram: .
Comment:

I have a really large textfile in which I want to find a certain, well defined string. What I know is that this string appears at the end of my large textfile. Therefore, to save a lot of time, I want to read my file backwards to search for the string. How can I do this? "tail" can only read 256 lines and often I want to go further than that. I would prefer ksh, awk or perl answers because that is what I use. thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: October 12, 2005 at 07:51:35 Pacific
Reply:

I can think of 2 methods (Perl modules) but there are probably more.

First choice: File::ReadBackwards
http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm

Second choice: Tie::File
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm

Let me know if the examples in their documentation aren't enough.


0

Response Number 2
Name: marklunda
Date: October 12, 2005 at 22:03:26 Pacific
Reply:

Ok, thanks. ReadBackwards looks interesting.
Can anyone think of an idea not using a Perl module, or a complete user written code?


0

Response Number 3
Name: FishMonger
Date: October 13, 2005 at 07:29:18 Pacific
Reply:

If you use Perl, I can't think of any logical reason not to use one of the modules. Can you? Do you enjoy spending your time doing things the hard way by "reinventing the wheel"?


0

Response Number 4
Name: marklunda
Date: October 13, 2005 at 22:24:32 Pacific
Reply:

Of course not! I work at company where I can't control/change the installed version of PERL. It would take months to apply for a change of the PERL installation. And since, ReadBackwards (which would do what I need, I'm sure) demands installation of files I'm looking for other ways. The easiest way would be if tail -1000 would work but that command is equal to tail -256 and therefore I can't match the string if it is on line 257 from the end. Another reason is that I'm pretty new to Perl and would prefere awk or ksh because that what i've been using over the years. So thats why I want to reinvent the wheel ... :-) Any suggestions?


0

Response Number 5
Name: FishMonger
Date: October 14, 2005 at 01:31:52 Pacific
Reply:

You can install the module in any directory that you have write access then add that directory to the @INC array.

#!/usr/bin/perl -w

use strict;
use lib '/path/to/module';
use File::ReadBackwards;

Another option is to use the Tie::File module and loop though the array in reverse. The Tie::File module is a core module, so worries about needing to install it.

#!/usr/bin/perl -w

use strict;
use Tie::File;

tie my @array, 'Tie::File', 'file.txt' or die $!;

for (my $i = $#array; $i >= 0; $i--) {
print "line $i: $array[$i]\n";
}

~~~~~~~~~
I don't do any shell scripting, so I can't help in that area, but I can say it won't be nearly as efficient. Shell scripts aren't very good at text processing of this type. As an example, a co-worker wrote a 120 line shell script to change the case of html file names and fix the links in the files. These files were spread through a couple dozen directories and totaled over 5,000. After 5 hours the script was still running. I wrote a 30 line Perl script to do the same thing and it only took 5 seconds to complete.


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: October 14, 2005 at 01:33:13 Pacific
Reply:

so worries

should have read

so NO worries


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Parse file backwards?

Log file parsing www.computing.net/answers/programming/log-file-parsing/16177.html

Parsing Text files www.computing.net/answers/programming/parsing-text-files/17102.html

Need Batch File for CSV file www.computing.net/answers/programming/need-batch-file-for-csv-file/16278.html