z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Searching files by using pattern matching

z/OS UNIX System Services User's Guide
SA23-2279-00

One of the most common record-keeping operations is obtaining a sublist of a list. For example, you might want to list all the Watchmen comics that appear in the main comics list. The command to do this is grep.

The simplest form of the grep command is:
grep word file
where word is a particular sequence of characters that you want to find, and file is your list of records. grep lists every line in the file that contains the given word. For example:
grep Watchmen comics.lst
lists every line in comics.lst that contains the word Watchmen. As another example:
grep 1986 comics.lst
lists every line in comics.lst that contains the sequence of characters 1986. Presumably, this lists all the comics that were published in 1986.
grep Jul:1986 comics.lst
lists all the comics published in July 1986.
If the string of characters you want to search for contains a blank, put single quotation marks (apostrophes) around the string; for example:
grep 'Dark Knight' comics.lst
You can save a sublist created by grep in a file using redirection:
grep Elektra comics.lst >el.lst

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014