search keyword in file with line number using grep command

grep command which is used for searching the keyword in file and show its output as lines containing a match to the given PATTERN or Keyword. When we simply use grep command without using any switches it simply shows the output with line having the keyword.Today,in this post we will know about a small tip to search keyword in file with line number using grep command .

To get the line number by using grep command. Use the below given syntax.

Syntax:

-n = switch to display line number

grep -n KEYWORD file-name

Example:

In below given example. You can see, 7: 11: 18: these refer as line number whereas keyword AllowOverride is found in file /etc/apache2/sites-enabled/000-default

root@tuxworld:~# grep -n AllowOverride /etc/apache2/sites-enabled/000-default 
7:		AllowOverride All
11:		AllowOverride All
18:		AllowOverride None
root@tuxworld:~#

Leave a Comment

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