Linux Grep Command with Options

Linux Grep Command

The grep command is used to searching string(s), regular expression in the file(s). There are many options with this command, which help you to make your searching perfect.

Using Linux Grep Command with Options

The following are the various variations (with options) of Linux grep command:

  1. grep "string" file
  2. grep -i
  3. grep -A n
  4. grep -B n
  5. grep -C n
  6. grep -o
  7. grep -o -b
  8. grep -n
  9. grep -r
  10. grep -c
  11. grep -v
  12. grep -e

1. grep "string" file

The primary task of this command is used to search string(s) with in the file.

This command displays the all lines which has the same string.

Example

ih@linux:~$ cat ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
LINE3: This is the line 3.
Enjoy learning online tutorials.

ih@linux:~$ grep “This” ok.txt
This is line 1.
LINE3: This is the line 3.

2. grep -i

The -i is used to case insensitive searching and display all lines which has the same string but ignoring the case.

Example

ih@linux:~$ grep -i “This” ok.txt
This is line 1.
THIS IS LINE 2.
LINE3: This is the line 3.

3. grep -A n

Displays n lines after the string matched.

Example

ih@linux:~$ grep -A 2 "1" ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com

4. grep -B n

Displays n lines before the string matched.

Example

ih@linux:~$  grep -B 2 "Enjoy" ok.txt
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.
Enjoy learning online tutorials.

5. grep -C n

Displays n lines before & after the string matched.

Example

ih@linux:~$ grep -C 2 "Guys" ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.
Enjoy learning online tutorials.

6. grep -o

Displays only matched string.

Example

ih@linux:~$ grep -o "This" ok.txt
This
This

ih@linux:~$ grep -o -i "This" ok.txt
This
THIS
This

7. grep -o -b

Displays only matched string with their positions.

Example

ih@linux:~$ grep -o -b "This" ok.txt
0:This
78:This

8. grep -n

Displays the line numbers while displaying the result of command.

Example

ih@linux:~$  grep -n "This" ok.txt
1:This is line 1.
4:LINE3: This is line 3.

9. grep -r

To search string in all files in the same and sub directories.

Example

ih@linux:~$ grep -r "This" *

10. grep -c

To display total numbers of matched string.

Example

ih@linux:~$ grep -c "This" ok.txt
2

ih@linux:~$ grep -c -i "This" ok.txt
3

11. grep -v

To inverts the match, when you do not want to display those lines which has the matched strings, use this option.

Example

ih@linux:~$ grep -v "This" ok.txt
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
Enjoy learning online tutorials.

12. grep -e

To search multiple strings.

Example

ih@linux:~$ grep -e "This" -e "Guys" ok.txt
This is line 1.
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.