Finding/Displaying Lines That Contain a Pattern

To find and display lines in a file that contain a name or pattern, you can use the grep command (you learn where it gets its name later). For example, assume that you have an employee file in which each line contains the name and information about one employee. To display just the lines from that file that contain the name Smith, you would enter this:

$ grep Smith employee-file
Smith, Carl - 555-6379, hired 3-7-92, dept 6
Smith, Dennis - 555-3291, hired 8-27-96, dept 2
Smithers, Jane - 619/555-1720, hired 4-4-89, dept 2
$

In this output, notice that only lines containing the word Smith are displayed. The other lines are not displayed. Also notice in the last line that Smith was found as part of ...

Get Practical UNIX 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.