14 Practical examples of the grep command

grep is a command-line utility that allows searching of words and/or patterns in a file or group of files. Its name comes from the ed command g/re/p (globally search a regular expression and print). Grep does a global search by keyword or regular expression and print all matching lines on the standard output.

General syntax

$ grep [OPTIONS] PATTERN [FILE...]
$ grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

In the second form you can specify more than one pattern using several times the -e switch or reading the pattern from a file (-f)

Basic options:

  • -c: Count the number of matches.
  • -E: Interpret PATTERN as an extended regular expression.
  • -f: Gets the patterns from a file, one per line.
  • -i: Case insensitive search.
  • -l: Print the name of each input file where matches are found.
  • -n: Prefix the line number where matches are found.
  • -o: Prints only the part that matches.
  • -v: Invert the sense of matching, to select non-matching lines.

GNU extensions:

  • –color: highlight the word that matches with the color specified in the variable of environment GREP_COLOR, red by default
  • -r,-R: Read all files under each directory, recursively

Note: all options are not covered

From the file: tools.txt, which contains the following lines:

Grep was originally developed for the Unix operating system.
GNU GREP was written from scratch for the GNU operating system.
ngrep is a network packet analyzer written by Jordan Ritter.
find is a command-line utility that searches one or more directory trees of a file system.
The  top  program  provides a dynamic real-time view of a running system.
xargs - build and execute command lines from standard input.

We’ve developed the following examples:

1. Case sensitive search

$ grep Grep tools.txt 
Grep was originally developed for the Unix operating system.

2. Case insentive search

$ grep -i Grep tools.txt 
Grep was originally developed for the Unix operating system.
GNU GREP was written from scratch for the GNU operating system.
ngrep is a network packet analyzer written by Jordan Ritter.

3. Count the number of matches (case sensitive)

$ grep -c Grep tools.txt 
1

4. Count the number of matches (case insensitive)

$ grep -ic Grep tools.txt 
3

5. Prefix line number

$  grep -in Grep tools.txt 
1:Grep was originally developed for the Unix operating system.
2:GNU GREP was written from scratch for the GNU operating system.
3:ngrep is a network packet analyzer written by Jordan Ritter.

6. Only the part that match

$ grep -io Grep tools.txt 
Grep
GREP
grep

7. Find lines containing xargs or top

$ grep -E 'xargs|top' tools.txt 
The  top  program  provides a dynamic real-time view of a running system.
xargs - build and execute command lines from standard input

8. Find lines that containing Grep (case insentive) and GNU (case sentive)

$ grep -i grep tools.txt | grep GNU 
GNU GREP was written from scratch for the GNU operating system.

9. Find all lines that don’t match grep

$ grep -iv grep tools.txt 
find is a command-line utility that searches one or more directory trees of a file system.
The  top  program  provides a dynamic real-time view of a running system.
xargs - build and execute command lines from standard input

10. Specify multiple patterns

Find all lines containing find or GNU

$ grep -e find -e GNU tools.txt 
GNU GREP was written from scratch for the GNU operating system.
find is a command-line utility that searches one or more directory trees of a file system.

11. Read patterns from a file

$ cat pattern.txt
Unix|GNU
grep

Do a regular expression search and find all lines containing Unix, GNU or grep

$ grep -Ef pattern.txt tools.txt 
Grep was originally developed for the Unix operating system.
GNU GREP was written from scratch for the GNU operating system.
ngrep is a network packet analyzer written by Jordan Ritter.

12. Highlight matching

The color of highlighted can be changed by setting the GREP_COLOR environment variable

$ export GREP_COLOR='1;37;43' &&  grep -i --color grep tools.txt

grep-colors

13. Anchoring

Find the exact word

$ grep -i '\bgrep\b' tools.txt 
Grep was originally developed for the Unix operating system.
GNU GREP was written from scratch for the GNU operating system.

14. Recursive search

Do a search for the word: Connection ignoring case and print the file list

$ grep -ilR Connection dbtool/
dbtool/src/DBTool/DB/Connection.php
dbtool/src/DBTool/DB/SqliteConnection.php
dbtool/src/DBTool/DB/ConnectionProperties.php
dbtool/src/DBTool/DB/ConnectionException.php
dbtool/src/DBTool/DB/MySqlConnection.php

Further readings




Leave a Comment

Your email address will not be published. Required fields are marked *