
UNIX® systems provide a useful command for searching for text within files. The command is named 'grep', for reasons which are unknown to us.
In its simplest form, the command syntax is:
grep text file
The command returns the whole of any lines in the file which contain the specified text.
For example:
$ grep John phone-numbers.txt John Hammonds: 01-639-203-791 John Millerchip: 01-541-201-363 $
You may specify more than one filename, or use wildcards such as '*.txt'. Grep includes many useful options, such as the ability to list the line number that the match(es) appear on, or to list only lines which do not contain the specified text. Look at the man page for 'grep', and try some examples it's a very useful command to get to know well.
Click here for the next Quick Tip
If this site has helped you, please consider sending us a small donation if you can afford it - thanks!