Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

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.pmSecond choice: Tie::File
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pmLet me know if the examples in their documentation aren't enough.

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

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"?

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?

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.

![]() |
![]() |
![]() |

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