Sajid Ali
Sajid Ali
Linux & Windows Server Administrator | Cloud Architect: I possess 10 years of professional experience with administration of different hosting provider’s Linux and Windows based servers on a large production level.

grep (Global Regular Expression Print) useful command-line utilities.

- Advertisement -

Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. It’s name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.

In the simplest terms, grep (global regular expression print) will search input files for a search string, and print the lines that match it. Beginning at the first line in the file, grep copies a line into a buffer, compares it against the search string, and if the comparison passes, prints the line to the screen. Grep will repeat this process until the file runs out of lines.

The grep command-line in Linux

Syntax:

grep [options] pattern [files]
  • OPTIONS – Zero or more options. Grep includes a number of options that control its behavior.
  • PATTERN – Search pattern.
  • FILE – Zero or more input file names.

To be able to search the file, the user running the command must have read access to the file.

Simple Example:

The simplest possible example of grep is:

# vi /etc/passwd

# grep "nobady" /etc/passwd

In this example, grep would loop through every line of the file “passwd” and print out every line that contains the word ‘nobady’:

Grep command-line
Grep command-line

Some Useful Options & Description:

-c : This prints only a count of the lines that match a pattern

# grep -c "nobody" /etc/passwd

-h : Display the matched lines, but do not display the filenames.

# grep -h "104.239.136.27" *

-i : Ignores, case for matching

# grep -i "NoBody" /etc/passwd

-l : Displays list of a filenames only. In the example it will display /etc/passwd.

# grep -l "104.239.136.2" *

-n : Display the matched lines and their line numbers.

# grep -l "104.239.136.27" *

-v : This prints out all the lines that do not matches the pattern, in this example line 18 will not print.

# grep -vn "nobody" /etc/passwd

-A n : Prints searched line and n lines after the result.

# grep -A5 "nobody" /etc/passwd

-B n : Prints searched line and n line before the result.

# grep -B5 "nobody" /etc/passwd

-C n : Prints searched line and n lines after before the result.

# grep -C5 "nobody" /etc/passwd

Regular Expression:

GNU Grep has three regular expression feature(https://linuxize.com/post/regular-expressions-in-grep/) sets, Basic, Extended and Perl-compatible.

By default, grep interprets the pattern as a basic regular expression where all characters except the meta-characters are actually regular expressions that match themselves.

^ : Use the ^ (caret) symbol to match expression at the start of a line. In the following example, the string “re” will match only if it occurs at the very beginning of a line.

# grep -in "^re" DesktopClient.ovpn

$ : Use the $ (dollar) symbol to match expression at the end of a line. In the following example, the string “nobody” will match only if it occurs at the very end of a line.

# grep -in "nobody$" openvpn-install.sh

. : Use the . (period) symbol to match any single character. For example, to match anything that begins with De then has two characters and ends with the string an, you could use the following pattern:

# grep -in "De..an" openvpn-install.sh

[ ] : Use [ ] (brackets) to match any single character enclosed in the brackets. For example, find the lines that contain accept or “accent, you could use the following pattern:

# grep -in acce[np]t Deb_file

Saving grep output to a file:

Take the output of a command and redirect it into a file using “>”

# grep -in "nobody" /etc/passwd > nobody_file

# vi nobody_file

Conclusion

The grep command allows you to search for a pattern inside of files. If a match is found, grep prints the lines containing the specified pattern.

There’s lots more to learn about grep man page.

- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook

x