Magazine

Pattern Searching Using Grep

Posted on the 21 March 2021 by Satish Kumar @satish_kumar86

The command g/RE/p stands for globally search for the regular expression (RE) and print the line. The return statuses are 0 for success, 1 for pattern not found, and 2 for file not found:

$ 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" *          // show only file names containing text //   
$ grep -ir "text" dir_name    // show lines of files //

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

Meta-character

Function

Example

Description

^

Beginning-of-line anchor

'^mango'

Will display all lines beginning withmango

$

End-of-line anchor

'mango'$'

Will display all lines ending withmango

.

Matches a single character

'm..o'

Will display lines containingm, followed by two characters, followed by ano

*

Matches zero or more characters preceding the asterisk

'*mango'

Will display lines with zero…


Back to Featured Articles on Logo Paperblog