Home » Unix command and scripts » How to use grep command in Unix and Linux with examples

How to use grep command in Unix and Linux with examples

grep command Means – globally search regular expression. It is very useful while searching for strings in Unix  and Linux operating system. Here we would be taking  a look on grep command in Unix with examples,Linux grep examples,grep command options,egrep command in Unix

How to use grep command in Unix

The grep utility searches text file.txt for a pattern and prints all lines that contain that pattern.

Syntax: grep [ -options ] limited-regular-expression [filename … ]

grep options or grep command options

grep command options

Unix grep command examples

(1) To find all uses of the word “top” (in any case) in the multiples file like x*, and write with line numbers(“grep -n”)

grep -i -n top x*
grep line number command example

(2) search ‘tmpfile’ for ‘CAT’ anywhere in a line

grep CAT tmpfile
grep command in unix with examples

(3) grep case insensitive search. By default grep command is case sensitive. You can use the option “grep -i” to make it case insensitive. 

grep -i Ten test.txt

grep case insensitive command

(4) find ‘run time’ or ‘run-time’ in all txt in file.txt

grep run[- ]time *.txt

(5) pipe who to grep, look for applmgr

who | grep applmgr

(6) grep recursive option(“grep -r”). Its searches for oracle string in current directory files and all the files in a subdirectory

grep -r "oracle" *
grep recursive command

  (7) Grep exclude option (“grep -v”) . We can use grep -v to exclude the search item. It will not show the lines which have oracle string in it

ps -ef|grep -v oracle

(8) We can use the “grep -w” option for searching the specific work, not the sub-string. The below example  searches adpatch.log for word failure in any case

grep -w failure adpatch.log

(9) You can search multiple words in a file using “grep -e”

grep -e tom -e bob  -e bill file.txt

This can also be done like this also

grep 'tom\|bob\|bill\|'  file.txt

Understanding Regular Expressions:

grep regular expressions

grep regex examples

(1)search file.txt for lines with ‘kite’

grep kite file.txt

(2) ‘kite’ at the start of a line

grep '^kite' file.txt
grep regex command

(3) ‘kite’ at the end of line

grep 'kite$' 
grep regex examples

(4)lines containing only ‘kite’

grep '^kite$'

(5)lines starting with ‘^s’, “\” escapes the ^

See also  if condition in shell script

grep '\^s' file.txt
(6)Search for ‘kite’ or ‘Kite’

grep '[Kk]ite' file.txt

(7)search for TOM, Tom, TOm, or ToM

grep 'T[oO][mM]' file.txt

(8)search for blank lines

grep '^$'

(9)search for pairs of numeric digits

grep '[0-9][0-9]' file

(10)list your mail

grep '^From: ' /usr/mail/$USER

(11)any line with at least one letter

grep '[a-zA-Z]' 1.txt

(12)anything, not a letter or number

grep '[^a-zA-Z0-9]'

(13)lines with exactly one character

grep '^.$' 

(14) ‘kite’ within double quotes

grep '"kite"'

(15) ‘kite’, with or without quotes

grep '"*kite"*'

(16)any line that starts with a Period “.”

grep '^\.' 

(17) line starts with “.” and 2 lower case letters} letters

 grep '^\.[a-z][a-z]'

egrep command in Unix

If you want to search multiple words in the same grep command, then use the egrep command in UNIX
It searches all the three words in the file

egrep  'cat|bad|sat'  file.txt

It discarded all the lines having any of these three words from the output of ps -ef

ps -ef|  egrep  -v  'cat|bad|sat'    :
egrep command can be used to select expression at the same time

grep with pipe command

pipe command in Linux lets u input the output of one command to the other command.

Example

ps -ef|grep python

Here the output of the “ps -ef” command is input for the grep command

 pipe command can be used to show the listing of processes in the OS

Some more Important Grep commands

(1) Sometimes we just want the grep to show out only the file names which matched the given pattern then we use the -l (lower-case L) option. if multiple files are there. This will simply print all the file names. Example of “grep -l”

grep -l ORA-0600 *.trc

(2) Suppose you want to count how many lines matches the given pattern/string, then use the option -c (“grep -c”)

grep -c "TOM" 1.txt

 (3) When you are searching error using grep on a huge file, it may be useful to see some lines around the match.

See also  How to use sed replace string in file

Lines before the match 

grep -A 10 "TOM" 1.txt 

Lines after the match 

grep -B 10 "TOM" 1.txt 

Lines around the match

 grep -C 10 "TOM" 1.txt 

(4) When we want to show the line number of the matched pattern within the file. we can use “grep -n”

grep -n "ORA-0600" alert.log

(5) Grep excludes directory in recursive search. Sometimes we want to exclude one directory from grep recursive search

 grep -r  --exclude-dir=log "TOM" *

Conclusion

grep is a very useful command for search word, expression in the Unix operating system. Hope you like this post on  grep command in Unix with examples

Related Articles

awk command examples in Unix: Awk command in Unix or Linux is a powerful command for processing text. Learn about awk syntax, records, fields, line Separator with examples of awk in Unix
sed command in Linux with examples: sed command is a Stream Editor – works as a filter processing  input line by line And here are 32 Useful sed command examples in Linux/Unix
find command examples in UNIX: find command in Unix with an example, How to use find command in Unix. Unix find directory command, how to find based on modified time
Linux command for Oracle DBA: This page has useful Unix command for Oracle DBA to help you in your day-to-day activities. The same is applicable for Linux also. How to kill the process
How to use tar command in Unix: how to tar a directory in Linux, tar command example, tar compress directory, tar gzip command, untar/extract command, tar cvfz command, tar xvfz command
windows grep equivalent: windows grep equivalent to search text: Find | findstr | power select-string. Detailed explanation with lots of examples for easy understanding
copy directory Linux: Check out this post the detailed description on how to copy file/directory in Linux. Examples of copy directory Linux command is also given
Knowledge base about Grep

2 thoughts on “How to use grep command in Unix and Linux with examples”

  1. Pingback: Have You Been Hacked? How to Clean Your Site and Get Off Google’s Blocklist

Leave a Comment

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

Scroll to Top