HomeHOWTOSHow to use the grep command in Linux

How to use the grep command in Linux

This guide will demonstrate how to use the grep command in Linux. In this post, we will utilize many examples to demonstrate how to use the grep utility. Grep (global regular expression print) is the most powerful and widely used command-line application in Linux.
You can use Grep to search for helpful information by defining search criteria. It looks for a specific expression pattern in a given file.
When a match is found, it prints all of the lines in a file that match the provided pattern. It is useful when you need to filter through enormous log files.

The basic syntax of grep command;
grep [options] PATTERN [FILE...]

For my case i’ll use sample.txt as a demo.

$ cat sample.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
Hey let's switch to linux

To print every field in a file that contains the word ‘linux’: Use “– i“which instructs the grep tool to ignore case sensitivity.

$ grep -i "linux" sample.txt
Hey it's linux
Linux is a farmily of open-source Unix-like operating system the kernel
linux is the best operating system when it comes to security 
Hey let's switch to linux

”-c” returns the number of occurrences of the matched pattern in a file.

$ grep -c "farmily" sample.txt
1

Use ” *” to look for all files in a directory that contain a specific pattern. It displays the specified files as well as the match results.

$ grep "source" * 
sample.txt:Linux is a farmily of open-source Unix-like operating systemon the kernel

Use the -n option to number the lines where the string pattern is matched, as illustrated.

$ grep -n "Linux" sample.txt 
2:Linux is a farmily of open-source Unix-like operating systemon the kernel

Use the – w to look for whole words in a file.Grep by default matches a string or pattern even if it appears as a substring in a file.

$ grep -w "system"  sample.txt
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 

Use the -o option to display the matched pattern. Grep by default shows the complete line containing the matched string.

$ grep -o "linux" sample .txt
linux
linux
linux
linux
linux
linux

Use – n to display the file’s line number where the matching line is found.

$ grep -n "linux" sample.txt
1:Hey it's linux
4:linux is the best operating system when it comes to security 
5:Hey let's switch to linux

Use – v option to invert the match pattern. The lines not found by the supplied search query can be displayed.

$ grep -v "Unix" sample.txt
Hey it's linux
An operating system kernel first released on september 17,1991 
linux is the best operating system when it comes to security 
Hey let's switch to linux

Use “^” to display the match string. The beginning of a line is specified by a regular expression pattern as shown.

$ grep "^linux" sample.txt
linux is the best operating system when it comes to security 

Use $ to display the match of a certain string. The end of a line is specified using a regular expression pattern as shown.

$ grep "kernel$" sample.txt
Linux is a farmily of open-source Unix-like operating systemon the kernel

To show the number of lines before and after a match.

  • -A :select the number of lines to display after a match.
  • -B :select the number of lines to display before a match.
  • -C :define the number of lines to display before and after a match.

Syntax:

grep -A[NumberOfLines(n)] [search] [file]  
grep -B[NumberOfLines(n)] [search] [file]  
grep -C[NumberOfLines(n)] [search] [file]  

I.e

$ grep -A1 Linux sample.txt
Linux is a farmily of open-source Unix-like operating systemon the kernel
An operating system kernel first released on september 17,1991 

Use – e to show multiple match patterns can be specified in the same line.

grep -e “Iso” -e “ job” -e “geek” sampl.txt

Use – m to limit the amount of lines returned by grep.

$ grep -m1 "linux" sample.txt
Hey it's linux

 $ grep -m2 "linux" sample.txt
Hey it's linux
linux is the best operating system when it comes to security 

Use -R option to recursively outputs the searched pattern in all files in the supplied directory.

Syntax:

grep -R [Search] [directory]

I.e

 $ grep -iR farmily  /home/ezra
"Linux is a farmily of open-source Unix-like operating systemon the kernel

i– has been used the above to find a string case-insensitively

Verdict

The grep tool is extremely versatile, and many novice Linux or Unix users find it difficult to understand.

For more visit; online at GNU.org too

Other tutorials:

Install KDE Desktop Environment on Ubuntu 22.04|20.04

How To Install TeamViewer on Ubuntu 22.04|20.04

How To Install Google Chrome on Ubuntu 22.04|20.04

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here