Sunday, September 5, 2010

UNIX look command to print lines


Let me introduce a simple UNIX/Linux command which is very useful but not that popular.
Its called 'look'. And as the name suggests, this command displays lines beginning with a given string.

Usage:

look string <file>

The look utility displays any lines in 'file' which contain 'string' as a prefix. This can be a simple alternative of using

$ grep "^string" <file>

If file is not specified, the file /usr/share/dict/words is used, only alphanumeric characters are compared and the case of alphabetic characters is ignored.
And I feel this lookup of dictionary words from 'usr/share/dict/words' can be a very common use of this utility.
e.g.

$ look advis
advisability
advisability's
advisable
advise
advised
advisedly
advisement
advisement's
adviser
adviser's
advisers
advises
advising
advisor
advisories
advisors
advisory

In order to ignore the case of alphabetic characters you can use its -f option. (similar to grep -i)

Exit Status:
The 'look' utility exits 0 if one or more lines were found and displayed, 1 if no lines were found, and >1 if an error occurred.

Relates posts:
- Generate random word in UNIX/Linux from dict/words
- UNIX - grep and print control characters from file
- Perform exact match with UNIX grep command
- UNIX grep command - print only matched string and not line
- Highlight match with color in grep

No comments:

© Jadu Saikia www.UNIXCL.com