Grep Command Complete Tutorial

Grep Command in Unix with 15 examples (Complete details)

 

The meaning of Grep or we can say a full form of grep is Global regular expression print. It is one of the most useful commands in Linux/Unix 

What you will learn after reading the article of Grep Command.

  • Usage and importance of grep command in linux and unix systems
  • Frequently asked questions related to grep command which generally in mind
  • You will learn practical examples and usage of grep command
  • We will provide all grep command options in single tabular format as well
  • You will be able to search the pattern in linux and unix systems after understanding the article

Usage and Importance of Grep Command

Unix/Linux is command line operating systems.

It is widely used in banking, telecom, and so many many sectors.

We need to search the files, patterns  in a text files, directories and many other system folders in our unix system. For this purpose Grep Command will be in used by professionals, students nd unix syetem users.

Frequently Asked Questions related to Grep Command

  1. What is grep command?
  •  Grep command is used in unix/linux system for searching the files in directory or searching the pattern in a file.

    Note: The acronym of grep is global regular expression print

   2. How does grep command work?

  • Grep command filters the file data or file name according to your search and it gives the result of matched pattern.

    Note:  Syntax of Grep command: grep options pattern filename

Syntax of grep command : You will able to understand in details, when we talk about examples of grep command in this article.

   3: How do you use grep command?

  • You can use grep command in any unix/linux terminal for searching the file or data in a file with the help of above syntax.

   4: How do you use grep recursively?

  • sometimes we need to search the file or any pattern under the directory as well and we don’t know in which file or directory there may be data,  then we need to search the pattern recursively.

For searching the data, you need to use -r option in grep command

 

In this article, Further you will learn about, grep command examples, grep command options, how to use it with syntax, so keep reading……

Log file creation for doing practice of grep command

Create the file naming log.txt.

You can create the file using touch command.

Grep Command Create file

Touch command will create the blank file and having no data.

So we need to insert the data in a file using the command

cat > log.txt (enter)

Unix is operating system.
Unix is most secure operating system.
unix is used in banking, telecom and in financial sector.
unix is free operating system.
unix is powerful.
unix is easy to use.
unix is multiuser operating system.

(Data which needs to be inserted)

Control +d 

grep command log file

You can check the data in alog file using the command

cat log.txt command.

Grep command Check data in Log file

Grep Command Examples with command execution.

Grep command Example 1:

How to find the last executed grep command on terminal?

Command: !grep

This will display the last executed grep command on terminal with results.

Last command we used : grep “unix” log.txt

when we will execute the command, !grep , it displayed the last command and the output. See below screenshot.

grep command example

Grep Command Example 2:

How to search a string in a file?

File name we are using: log.txt
pattern to search: system
Command: grep “system” log.txt

Above command will display the all lines of log.txt file name having system in it.

See below screenshot:

grep command example

Grep Command Example 3:

How to search a string in multiple files with single grep command?

File names: log1.txt, log2.txt , log3.txt
pattern to search: system
Command: grep “system” log1.txt log2.txt log3.txt

We have created the files log1.txt ,log2.txt, log3.txt and added some randome data in it.

Output of command:

$ grep “system” log1.txt log2.txt log3.txt
log1.txt:Unix is operating system.
log2.txt:unix is multiuser operating system.
log3.txt:unix is multiuser operating system.

You will see the file name: pattern you got in output.

Also check below screenshot.

grep command example

Grep Command Example 4:

How to search a string in a file case insensitive (ignore cases in grep search)?
File name: log.txt
pattern to search: system
Command: grep -i “unix” log.txt
for multile files
command: grep -i “unix” log1.txt log2.txt log3.txt

Here -i is used for search with case insensitive search

Output you will got for above 2 commands:

91989@DESKTOP-2PVE7O2 MINGW64 ~/unix
$ grep -i “unix” log.txt
Unix is operating system.
Unix is most secure operating system.
unix is used in banking, telecom and in financial sector.
unix is free operating system.
unix is powerful.
unix is easy to use.
unix is multiuser operating system.

91989@DESKTOP-2PVE7O2 MINGW64 ~/unix
$ grep -i “unix” log1.txt log2.txt log3.txt
log1.txt:Unix is operating system.
log2.txt:unix is multiuser operating system.
log3.txt:unix is multiuser operating system.

You will see there is capital U, but it came in search results. This is due to -i

See below command execution in gitbash

 

grep command example

Grep Command Example 5:

How to search the pattern starting with number i.e 0 to 9

command: grep “^[0-9].*” log.txt
the above command search for lines which start with number,
also you can change the number or use strings to search the regular expressions in lunix and unix systems.

For this purpose, I have edited the log.txt file and some lines started with number now. when we execute the above command. I got the below output.

$ grep “^[0-9].*” log.txt

1 Unix is operating system.

2 Unix is most secure operating system.

4 unix is used in banking, telecom and in financial sector.

See the below command line execution of grep command:
grep command example

Grep Command Example 6:

how to search the pattern recursively?
Recursively means, we need to search the pattern in all files.
option -r is used with grep command to search the pattern recursively
command : grep -r “string” *

pattern to search: unix

command we will use: grep -r “unix” *

Output we will got:

91989@DESKTOP-2PVE7O2 MINGW64 ~/unix
$ grep -r “unix” *
log.txt:4 unix is used in banking, telecom and in financial sector.
log.txt:unix is free operating system.
log.txt:unix is powerful.
log.txt:unix is easy to use.
log.txt:unix is multiuser operating system.
log2.txt:unix is multiuser operating system.
log3.txt:unix is multiuser operating system.

See below screenshot:

grep command example

Grep Command Example 7:

How to display the lines before the match pattern?

command : grep -B 2 “free” log.txt

This will display the lines where the pattern free match along with the 2 lines before as well.

Output:

$ grep -B 2 “free” log.txt
2 Unix is most secure operating system.
4 unix is used in banking, telecom and in financial sector.
unix is free operating system.

Grep command example 8:

How to display the lines after the match pattern?
Command : grep -A 2 “free” log.txt

the above grep command will display the lines where the pattern free match along with the 2 lines after as well.
Output:

$ grep -A 2 “free” log.txt
unix is free operating system.
unix is powerful.
unix is easy to use.

grep command example

Grep command Example 9:

How to display the lines around (before and after)the match pattern?
command : grep -C 3 “free” log.txt

Output:

$ grep -C 3 “free” log.txt
1 Unix is operating system.
2 Unix is most secure operating system.
4 unix is used in banking, telecom and in financial sector.
unix is free operating system.
unix is powerful.
unix is easy to use.
unix is multiuser operating system.

That displays the 3 lines above the matched pattern (free) and below the pattern (free) along with the pattern.

Grep Command Example 10:

How to check the whole word in a file?
command : grep -w “in” log.txt
grep command gives the result of pattern if it is a substring as well. option -w is used for matching the only whole words.

If we use without -w , we got that output

$ grep “in” log.txt
1 Unix is operating system.
2 Unix is most secure operating system.
4 unix is used in banking, telecom and in financial sector.
unix is free operating system.
unix is multiuser operating system.

When we use with -w, we got only  output for whole word.

$ grep -w “in” log.txt
4 unix is used in banking, telecom and in financial sector.

 

grep command example

Grep Command Example 11:

How to invert the pattern search?

Command: grep -v “system” log.txt
By using the option -v, it will display lines of log.txt file which don’t have system word in it.

Output:

$ grep -v “system” log.txt
4 unix is used in banking, telecom and in financial sector.
unix is powerful.
unix is easy to use.

Grep Command Example 12:

How to count the number of matches using grep command?

Command: grep -c “system” log.txt

By using -c in grep command it will display the count of “system” in log.txt

Output:

$ grep -c “system” log.txt
4

grep command example

Grep Command Example 13:

How to display the file names that contains the pattern?

Command: grep -l “free” *

It can just dsiplay the file names that conatins the pattern free in it. It checks in current directory.

Output:

$ grep -l “free” *
log.txt

grep command example

Grep Command Example 14:

How to display the file names that do not matches the pattern?


Command: grep -L “free” *


Here we are using Uppercase -L to find the file names that do not matches the pattern.

Output:

$ grep -L “free” *
log1.txt
log2.txt
log3.txt
Unix

grep command example

Grep Command Example 15:

How to remove the blank lines using the grep command?

Command: grep -v “^$” log.txt

By using the -v and “^$” will remove the empty and blank lines from the file.

Grep Command Example 16:

How to display only the matched pattern using grep command?

Command: grep -o “string” file.txt

Grep Command examples summary:

OptionsDescription
-c  This prints only a count of the lines that match a pattern, Not lines of matched pattern
-h It will display the matched lines, but do not display the filenames.
-i It is used for case insensitive search
-l It will display the filename only.
-n It will display their line numbers and the matched lines.
-v It will display those lines whose pattern is not matching.
-e expIt ppecifies expression with this option. Can use multiple times.
-f fileIt takes patterns from file, one per line.
-e It assumes as extended regular expression (ERE)
-w It matches whole word only, (Not combine 2 words)
-o It will print only the matched parts of a matching line,  

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.