25 Linux Grep Command Examples Should try in work place

By | January 19, 2017
25 Grep Linux Examples

25 Grep Linux Examples

Linux Grep command is mostly used to search for Text Pattern’s/Specific words in set of files.Most of the people dont know the full form of Grep – grep is Global regular expression print.Grep command processes each line by line and prints line which matches the pattern.

Let’s go Examples,which we are most interested in.

Usage 1: Search for word in a File

## grep password /MyFolder/SystemLog.txt

With the above command ,i am trying to print any lines which has “password”.But it does not search for upper case “PASSWORD”.

Usage 2: Search for word in a File,ignore case sensitive

## grep -i password /MyFolder/SystemLog.txt

With the above command we can search “password” / “PASSWORD” in a file.

Usage 3: Search for word in a Files recursively

## grep -r password /MyFolder

With the above command we can search “password” in MyFolder along with its sub folders too.

Usage 4: show line number with your search results.

## grep -n password /MyFolder

Usage 5: show position of match in line number.

## grep -n password /MyFolder/SystemLog.txt

say password is matched in SystemLog.txt at 20th character.We will get the 20 and line number in results.

Usage 6: show only matched string.

## grep -o password /MyFolder/SystemLog.txt

By default,it shows complete line with matched string.with the above command ,we only get the matched word in results.

Usage 7: Find valid IP Address using regular expression.

## cat myInputFile

255.255.255.124
1.2.3.4
192.168.1.2

$ egrep  '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' input
255.255.255.124
192.168.1.2
Usage 8: Search for multiple patterns for single word.
## fgrep –f file_with_patterns.txt file_to_search.txt

Normally we search for only one pattern.But with fgrep we can give multiple patterns for searching string.

Usage 9: Search for String in Zip Files.

## zgrep –i warning /var/log/Logs.gz

In linux,we zip the files with extension as “.gz”.We can search in Zip file by using above command.

Usage 10: Search for only String,means we dont want subset kind of results.

## cat myfile| grep –w “cook”

with the above command,we will exactly find out the searches which have “cook” word.It wont give results which has cooking.

Usage 11: How many times word is matched in search.

## cat myFile| grep –c SearchString

we get search word followed by it’s count.

 

Usage 12: print lines before the match.

## cat myFile|grep –B 2 ERROR

It just prints 2 lines before the “ERROR” String.

Usage 13: print lines after the match.

## cat myFile|grep –A 2 ERROR

It just prints 2 lines before the “ERROR” String.

Usage 14: count number of matches .

## grep -c "ERROR" application.log

It just prints count of ERROR word in the application.log.

Usage 15: Grep output  in colour

## grep --colour password /MyFolder/SystemLog.txt

With the above command ,i am trying to print any lines which has “password”.

It will highlight the results where the “password” in colour.

Usage 16: succesful results with line number in grep output

## grep -n password /MyFolder/SystemLog.txt

With the above command ,i am trying to print any lines which has “password”.

It will highlight the results where the “password” with line number

Usage 17: How to search in multiple files with wildcard

## grep -i "ERROR" *

It will search in all the files for the ERROR Word.

Usage 18: How to search in multiple files  with extension of “.log” only

## grep -i "ERROR" *.log

It will search in all the files which has extension of .log only for the ERROR Word.

Usage 19: How to search for lines which has space as first character.Space means it can be tab or new line or some spacess.

## grep "^[[:space:]]" myInputFile

Usage 20: How to search for set of range numbers in file using regular expression.

## cat myInputFile

10

300

5000

60000

grep "^[0-4]\{1,3\}$" myInputFile

the above grep command will give the result with number with 1 digit or 2 digit or 3 digit and it should contain numbers only 0,1,2,3 and 4 only.

output: 

10

300

Usage 21: How to search for exact numbers with size of “n” numbers in file using regular expression.

## cat myInputFile

10

300

4000

8000

67809

grep "^[0-4]\{4\}$" myInputFile

the above grep command will give the result with exact four numbers in size and that four numbers also should be in between 0 – 4.

output: 4000

Usage 22: How to search lines which starts with only alpha numeric using regular expression.

grep "^[[:alnum:]]" MyInputFile

the above command searches for lines which starts with [a-z] or [A-Z] or [0-9] in file MyInputfile

Usage 23: How to search lines which starts with only alpha using regular expression.

$ grep "^[[:alpha:]]" MyInputFile
the above command searches for lines which starts with [a-z] or [A-Z] in file MyInputfile

Usage 24: How to search lines which starts with only numbers using regular expression.

grep "^[[:digit:]]" MyInputFile
the above command searches for lines which starts with only number [0-9] in file MyInputfile

Usage 25: print lines before and after the match.

## cat myFile|grep –C 2 ERROR

It just prints 2 lines before  and after the “ERROR” String.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply