bsearch searches the sorted data file inputfile for a line or lines matching key. Binary search is used, performing random-access seeks within the file, not all of which therefore needs to be read. Binary searches, even on huge files, can be quite efficient.
By default, the search is for a line which contains key in its first (whitespace-separated) column. A few other variants on this basic search strategy are also supported, selected by option flags as described below.
The input data file must obviously be sorted, and moreover, bsearch must know how it was sorted. bsearch therefore accepts many of the same option flags as does sort(1), indicating whether the data file being searched was sorted numerically, or caselessly, or on a particular column. In general, bsearch should be given more or less the same option flags when searching as sort(1) was when generating the file being searched.
A few combinations of options make no sense and are disallowed. Only one of -p, -w, and -x should appear. Only one of -br, -m, and -range should appear. Option -x precludes use of -k or +n to select a search column. Option -p may not be used with -n.
If the program is invoked via a link named look, the -m and -p options are assumed, the -w option becomes useful to override the implicit -p, the file /usr/dict/words is assumed if the filename parameter is omitted, and the -a option selects the alternate default dictionary file /usr/dict/web2 instead.
The exit status is zero if a match is found, 1 if no match is found (including when merely bracketing lines are printed using -br), and 2 for errors.
The numeric search (-n) works on integers only, not floating-point.
Not all of sort(1)'s options are accepted.
The handling of column specifiers is incomplete, at least as far as compatibility with sort(1) is concerned. The m.n notation for selecting a character position within a field is not supported. Only one search column may be specified. (A field-end specifier is ignored.)
bsearch essentially acts as if sort(1)'s -b flag is always in effect.
At most one column specifier is supported; it is not possible to specify addditional key(s) to search for in secondary (or tertiary...) sort columns.
See http://www.eskimo.com/~scs/src/#bsearch for possible updates.
Steve Summit, email@example.com