grep—searches a file for a pattern

grep [ –bchilnsvw ] limited–regular–expression [ filename ... ]

grep searches files for a pattern and prints all lines that contain that pattern. Uses regular expressions metacharacters to match the patterns. egrep has an extended set of metacharacters.

Example A.25.
1   grep Tom file1 file2 file3
2   grep -in '^tom savage' *

EXPLANATION

  1. Grep displays all lines in file1, file2, and file3 that contain the pattern Tom.

  2. Grep displays all lines with line numbers from the files in the current working directory that contain tom savage if tom savage is at the beginning of the line, ignoring case.

Get UNIX® Shells by Example, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.