Pattern searching using grep

The g/RE/p stands for globally search for the regular expression (RE) and print out the line.

Return status – success 0, pattern not found 1, file not found 2.

$  ps -ef | grep root

The preceding command will show all processes running currently whose user ID is "root".

$  ll /proc | grep "cpuinfo"

The preceding command will show the file with the name cpuinfo from the /proc directory.

$  grep –lir "text" *   // only file names  //   
$ grep –ir  "text" dir_name   // show lines of files //

We will try the following commands on the love.txt file:

Metacharacter

Function

Example

Description

^

Beginning-of-line anchor

'^mango'

Will display all lines beginning with mango

$

End-of-line anchor

'mango'$'

Will display ...

Get Learning Linux Shell Scripting 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.