Search file from 'bottom to top', varying search ?

Search file from 'bottom to top', varying search ?

Post by David Welto » Wed, 12 Mar 1997 04:00:00



I need to search a file from bottom to top so to speak, varying the
search each time it matches (thus, the reason why I don't just grep
it).  I'm wondering not exactly *how* to do this, but the best way.
So far, I see two ways - load up the whole file, reverse it, and then
proceed as if it were normal, or, search the whole file for my string,
use the last match to get the next search, and so on and so forth.
Currently, I'm working with perl, but I may later redo it in C.  

These methods both seem more than a little inefficient, and I wonder
if there isn't a better way, or if not, at least which one of these
two is best...  The first runs into memory problems, but the second
seems like quite a waste too.

Thanks for your thoughts,
--
David Welton  

Se quest'email e` in Italiano, mi dispiace per gli errori:-) FORZA PANTANI!
                                --Linuxista--

 
 
 

Search file from 'bottom to top', varying search ?

Post by Andrew Giert » Thu, 13 Mar 1997 04:00:00


 David> I need to search a file from bottom to top so to speak,
 David> varying the search each time it matches (thus, the reason why
 David> I don't just grep it).  I'm wondering not exactly *how* to do
 David> this, but the best way.  So far, I see two ways - load up the
 David> whole file, reverse it, and then proceed as if it were normal,
 David> or, search the whole file for my string, use the last match to
 David> get the next search, and so on and so forth.  Currently, I'm
 David> working with perl, but I may later redo it in C.

 David> These methods both seem more than a little inefficient, and I
 David> wonder if there isn't a better way, or if not, at least which
 David> one of these two is best...  The first runs into memory
 David> problems, but the second seems like quite a waste too.

In C, do it by mmaping the file and search backwards from the end. This
is an extremely neat trick (which I freely confess I did not think of
myself, until I saw a comment about it in the FreeBSD implementation
of 'tail'). This is about as efficient as it gets.

In Perl, I think you have a speed/space tradeoff. You might be able to
get a major win, though, by using 'tail -r' (if you have a version of
tail that supports -r) to reverse the input. You could snarf the FreeBSD
one; the GNU tail doesn't seem to support it.

Hope this helps

--
Andrew.

 
 
 

1. Sting search w/o 'find' or 'du'

I'm in a Unix course and I have a question. I need to write a
replacement 'find' command and I've got everything done but one. Given
that I have a list of files in a dir stored in a string array, how do
I search each array element to see if it contains a given substring? I
have to be able to determine if the sub string is contained within the
array element in any fashion (begin, middle, end, etc.)

I would appreciate any help with this. Either post here or email
directly to me. Thanks.

2. Help: Migrating to a bigger drive

3. Searching for a hex '0a' in text file

4. Sound Galaxy

5. Combining 'find' and 'grep' or searching in general

6. Copying vfat to vfat under Linux: Huge increase in size.

7. 'less' command doesn't work properly in searching strings...

8. need advice on secure servers

9. grep question...how do i search and highlight the search in a file??

10. getting rid of top lines of a file without searching through entire file

11. Search 'microsoft gates thief liar cheat steal'

12. Locked on boot at 'Searching for CD-ROM'

13. sndconfig 'sndcore.o not found in module search path'