in reply to fast lookups in files

If key and have fixed length (for example both 32 bit integers), you could write it into a binary and do a binary search.

I think there is also a small unix tool that does line based binary search in files, but I can't remember the name. Update: It seems look (1) (in Debian shipped with in the bsdmainutils package) does the trick, but I'm not sure if it works with numerically sorted lines.

Replies are listed 'Best First'.
Re^2: fast lookups in files
by bart (Canon) on Feb 05, 2008 at 13:01 UTC
    ...but I'm not sure if it works with numerically sorted lines.
    Then make a processed copy of the file, where you pad the numbers to the right to a fixed length, with sprintf, so they sort the same numerically and asciibetically.
      Right, but again it requires the size of the index to be known in advance - in which case you can use the binary data much more efficient.
        If you plan to store the numbers as binary data in the file: note that positive big endian integers also sort properly asciibetically. Use the "N" template for pack.