Using grep for text matching

Grep (short for Global Regular Expression Print) is a command that is used extensively to as a text search tool in text files. It searches for a pattern in a file and prints the corresponding line, which contains the matching pattern. It scans files for specified patterns and can be used with regular expressions, as well as text strings. Its syntax is as follows:

$ grep [options] pattern [files]

The following table demonstrates when the grep command is used:

Command

Usage

grep 'student' /etc/passwd

Search for a string, student, in a file, /etc/passwd, and print all matching lines

grep -v 'student' /etc/passwd

Print all lines that do not contain the string student

grep -i 'STUDENT' /etc/passwd

Search for a string, STUDENT, in a case-insensitive manner and print all matching lines (-i ignore case)

grep -c 'student' /etc/passwd

Print the total number of lines that contain the text student in the /etc/passwd file

grep -rl 'student' /etc/

Search the directory recursively and print the filenames that have the string student

grep -rL ‘student’ /etc/

Search the directory recursively and print the filenames that don't have the string student

grep -n 'student' /etc/passwd

Print the line number, along with the line containing the pattern student

grep -A1 'student' /etc/passwd

Print an additional one line after the match

grep -B1 'student' /etc/passwd

Print an additional one line before the match

grep -C1 'student' /etc/passwd

Print an additional one line after, and one line before, the match

grep -a 'dir' /bin/mkdir

Search inside the /bin/mkdir binary file and print the line containing the string dir

grep 'root' /etc/passwd

Print the line containing the string root anywhere on a line

grep '^root' /etc/passwd

Print the line that begins with the string root

grep 'bash$' /etc/passwd

Print the line that ends with the string bash

grep '^$' <filename>

Print the empty lines from the file

grep -v '^$' <filename>

Print only non-empty lines from the file

grep '[br]oot' /etc/passwd

Print the lines that contain either string beginning with the characters b or r, and followed by the string oot, anywhere on a line in the /etc/passwd file

who | grep 'student'

Print the line containing the string student by reading input from stdin

 

An example of matching a string in a file using grep is shown in the following screenshot:

An example of printing those lines that do not contain the specified string using grep is shown in the following screenshot (some output stripped):

The grep command can be used with the -c option to count the occurrence of a specified pattern. The following example shows how to count the number of CPU cores in a system using grep command:

$ grep -c name /proc/cpuinfo (count the number of cpu cores in 
system)

The following screenshot shows how to use grep command to count the occurrence of root string in the /etc/passwd file:

An example of printing the line number, along with the matching lines using the grep, is shown in the following screenshot:

An example of printing the lines that begin with a specified string is shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.115.155