Home / Alt manpages / look(1)

  • look(1)
  • User command
  • linux

Search a Sorted Word List with look

Grep chokes on huge sorted files: look finds every line starting with a prefix almost instantly with a binary search. The examples use the installed manpage from Ubuntu's bsdextrautils package, version 2.39.3-9ubuntu6.6. The command found first in this shell is util-linux 2.41.3, so check your own version when exact release behaviour matters.

Allow about ten minutes. You need a shell and a text file whose ordering you control. This guide only reads files and writes a temporary example under /tmp; no elevated privileges are needed. Do not point a command that creates a sorted file at an important path until you have checked the output and destination.

1. Check the command and its assumptions

Confirm which implementation will run and read the concise help:

$ command -v look
$ look --version
$ look --help
  • It is a binary search, not a scan. The installed manpage describes look as a binary-search utility, not something that reads arbitrary text line by line.
  • The file must match its comparison rules. It has to be sorted the same way look compares it. Change the comparison options and you must give sort the matching options too.

Checkpoint

Record the path and version if a script or support note must be reproducible. A different executable earlier in PATH can explain a version mismatch without any change to the operating system package.

2. Create a small sorted word list

Use a harmless temporary file so the ordering and result are visible:

$ printf '%s\n' \
    'apple 10' \
    'application 20' \
    'banana 30' \
    'band 40' \
    'zebra 50' > /tmp/look-example.txt
$ sort -d /tmp/look-example.txt -o /tmp/look-example.txt
$ sed -n '1,5p' /tmp/look-example.txt
apple 10
application 20
banana 30
band 40
zebra 50

The redirection creates or replaces only the named temporary file. The final sort command updates that same file after sorting. With a real data file, write to a separate temporary path first, inspect it, then replace the original only if that change is genuinely intended.

3. Search for a prefix

Pass the prefix first and the file second:

$ look appl /tmp/look-example.txt
apple 10
application 20

Both lines come back because their beginnings match appl. This is not a regular expression: characters such as ., * and ? do not gain pattern syntax here. Reach for grep when you actually need regular expressions or the file isn't sorted.

Check the no-match case and its status too, because scripts live or die on this:

$ look kiwi /tmp/look-example.txt
$ printf 'status=%s\n' "$?"
status=1
  • Status 0 means one or more lines were displayed.
  • Status 1 means none were found.
  • Anything above 1 means an error occurred.

Capture $? immediately, before running another command, or you'll be capturing the wrong thing's exit code.

4. Search a colon-separated file by its first field

The -t option tells look where the searchable prefix ends, which matters for a file sorted by a delimited key:

$ sort -d -t: -k1,1 /etc/passwd > /tmp/passwd-by-name
$ look -t: root:anything /tmp/passwd-by-name
root:x:0:0:root:/root:/bin/bash

Only the text through the first colon in the search string is compared, including that colon itself. The remainder, anything here, is ignored for the lookup. Use a separate output file: the example does not touch the original /etc/passwd and does not need root access.

Do not assume a colon-separated file is ordered correctly just because it looks tidy. The sort key and comparison mode have to match the lookup. If the file came from another system, check its documented ordering before trusting a status 1 as proof that a key is absent.

5. Make dictionary matching explicit

Leave off a filename and look falls back to /usr/share/dict/words. The manpage says this default mode compares only alphanumeric characters, ignores case and uses dictionary ordering. The WORDLIST environment variable overrides that default path.

$ if [ -r /usr/share/dict/words ]; then
>     look -d -f net
> else
>     printf '%s\n' 'No readable default dictionary at /usr/share/dict/words' >&2
> fi

A missing dictionary is not fixed by adding sudo to look. Check the path, the WORDLIST value and the package that supplies your dictionary. For a portable command, pass an explicit file rather than relying on a host's default dictionary.

For a custom file, use -d and -f consistently on both commands:

$ sort -d -f /tmp/look-example.txt -o /tmp/look-example.txt
$ look -d -f APP /tmp/look-example.txt

These options change comparison. Sort without them and search with them, or the other way round, and you can get missed results even when the file looks sorted to a person.

Common traps and recovery

  • Unsorted input: recreate a sorted copy with the same sort options, then test a prefix whose expected lines are known.
  • Wrong file: print the path and inspect its first and last few lines before diagnosing a no-match result.
  • Confusing prefix and pattern search: use grep -F for fixed-string scanning, or grep for regular expressions. look is for ordered prefix lookup only.
  • Overwriting source data: sort to a new file first. If an example here created a temporary file, remove only those named files when you have finished: rm -- /tmp/look-example.txt /tmp/passwd-by-name. This cleanup is optional and does not touch the source files.

Done means

  • Checked the executable path and version.
  • Your input is sorted with the same comparison rules used by look.
  • A known prefix returned the expected lines.
  • You can tell apart a no-match status of 1 from an actual error.
  • You know whether the default dictionary and WORDLIST apply to your command.