LinuxCommandLibrary

look

Find lines beginning with a specified string

TLDR

Search for lines beginning with a specific prefix in a specific file

$ look [prefix] [path/to/file]
copy

Case-insensitively search only on alphanumeric characters
$ look [[-f|--ignore-case]] [[-d|--alphanum]] [prefix] [path/to/file]
copy

Specify a string termination character (space by default)
$ look [[-t|--terminate]] [,]
copy

Search in /usr/share/dict/words (--alphanum and --ignore-case are assumed)
$ look [prefix]
copy

SYNOPSIS

look [options] string [file ...]

PARAMETERS

-a
    Displays all lines that have string as a prefix, not just the first match.

-d
    Uses the specified file as the dictionary instead of the default dictionary file.

-f
    Performs a case-sensitive search. By default, look ignores case.

-t char
    Specifies a string termination character. Only the characters up to this terminator are compared,
useful for searching files with non-alphanumeric characters.

-b
    Binary search the file. This is used to search a pre-sorted file using binary search, without index generation.

DESCRIPTION

The look command searches a dictionary file (typically located at /usr/share/dict/words or specified with the -d option) for lines beginning with a given string. It's primarily used to find words or phrases that start with a specific prefix. The command performs a binary search, making it efficient for large dictionaries. By default, it ignores case, but this can be modified with the -f option. look is helpful for tasks like autocompletion, word games, or exploring vocabulary based on a starting pattern. The command reads the dictionary line by line and compares it against the provided prefix. If a match is found, the entire line (word or phrase) is printed to standard output. Several options enhance the search, like printing all lines that match as prefixes (-a) or using system collation rules (-c).

CAVEATS

look is optimized for sorted dictionary files. Using it on unsorted files will produce unpredictable results. The location of the default dictionary file may vary across different Linux distributions.

DEFAULT DICTIONARY

The default dictionary used by look is typically located at /usr/share/dict/words or a similar path. The exact location may vary based on the Linux distribution.

HISTORY

look has been a part of Unix-like operating systems for a considerable amount of time. It originated in the early days of Unix and has been included in many subsequent versions and distributions. The command's primary function, to perform quick lookups in dictionary files, has remained largely unchanged since its inception, making it a reliable tool for basic word searching tasks.

SEE ALSO

grep(1), fgrep(1), sort(1)

Copied to clipboard