• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Techgoeasy

Techgoeasy

Learn Oracle, PHP, HTML,CSS,Perl,UNIX shell scripts

  • Home
  • Oracle
    • Oracle database
    • Oracle Ebusiness Suite
    • Oracle weblogic
    • Oracle Performance Tuning
    • Oracle Hyperion
    • Oracle Cloud Tutorials
  • SQL
  • interview questions
  • Linux
  • PHP and HTML
  • Downloads
  • General
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

August 31, 2020 by techgoeasy 1 Comment


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

grep command in Unix

Table of Contents

  • How to use grep command in Unix
    • grep options or grep command options
  • Unix grep command examples
  • Understanding Regular Expressions:
  • grep regex examples
  • egrep command in Unix
  • grep with pipe command
    • Some more Important Grep commands
    • Conclusion

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

-cPrint only a count of the lines that contain the pattern.
-iIgnore upper/lower case distinction during comparisons.
-lPrint only the names of file.txt with matching lines,separated by NEWLINE characters. Does not repeat the names of file.txt when the pattern is found more than once.
-nPrecede each line by its line number in the file (first line is 1).
-vPrint all lines except those that contain the pattern.
-rIt recursively search the pattern in all the file.txt in the current directory and all it’s sub-directory.
-wIt searches the exact word

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 -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 command . By default grep command is case sensitive. You can use option grep -i to make it case insensitive.  We can use grep -w option for searching the specific work not sub-string . The below example  searches adpatch.log for word failure in any case
    grep -i -w failure adpatch.log
    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 appmmgr
    who | grep appmmgr
  6.    grep recursive option .It search for oracle string in current directory files and all the files in sub directory
    grep -r "oracle" *
    grep recursive command

  7.   Grep exclude option (grep -v) . We can use grep -v to exclude the search item item. It will not show the lines which has oracle string in it
    ps -ef|grep -v oracle

Understanding Regular Expressions:

^ (Caret)match expression at the start of a line, as in ^A.
$ (Question)match expression at the end of a line, as in A$.
\ (Back Slash)turn off the special meaning of the next character, as in \^. To look for a Caret “^” at the start of a line, the expression is ^\^.
[ ] (Brackets)match any one of the enclosed characters, as in [aeiou]. Use Hyphen “-” for a range, as in [0-9].
[^ ]match any one character except those enclosed in [ ], as in [^0-9].
. (Period)match a single character of any value, except end of line. So b.b will match “bob”, “bib”, “b-b”, etc.
* (Asterisk)match zero or more of the preceding character or expression. An asterisk matches zero or more of what precedes it. Thus [A-Z]* matches any number of upper-case letters, including none, while [A-Z][A-Z]* matches one or more upper-case letters.

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 a line
    grep 'kite
    grep regex examples
  4. lines containing only ‘kite’
    grep '^kite 
  5. lines starting with ‘^s’, “\” escapes the ^
    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 start 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 egrep command in UNIX
It search all the three words in the file

egrep  'cat|bad|sat'  file.txt

It discarded all the lines having any of these three word 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 let u input the output of the one command to the another command.

Example

ps -ef|grep python

Here output of “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
    grep -l ORA-0600 *.trc
  2. Suppose you want to count that how many lines matches the given pattern/string, then use the option -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.
    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 with in the file.we can use grep -n
     grep -n "ORA-0600" alert.log
  5. Grep exclude directory in recursive search. Some time 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 operation 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 example,How to use find command in Unix.Unix find directory command,how to find 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. Same are 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 example 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


Filed Under: Unix command and scripts Tagged With: grep, grep command options in unix, grep examples in unix, how to use grep command in unix, unix, unix grep command, unix grep examples

Reader Interactions

Comments

  1. Sherjeel Hasan says

    September 5, 2019 at 8:01 am

    How does one use the -F option of the grep command?

    Reply

Leave a Reply Cancel reply

Primary Sidebar

Top Posts & Pages

  • How to check Tablespace in Oracle -Size ,Free space,datafiles ,growth
  • how to check all constraints on a table in oracle
  • Query to check table size in Oracle database
  • Oracle dba interview questions and answers
  • Oracle Indexes and types of indexes in oracle with example



Subscribe to our mailing list

Enter your email address to subscribe to this blog and receive notifications of new posts by email

Recent Posts

  • How to run Autoconfig on Patch system in R12.2
  • Admin scripts in R12.2 Ebuisness Suite
  • Timezone settings in Oracle database
  • how to enable trace in oracle apps r12
  • Transparent Data Encryption Queries

Copyright © 2021 : TechGoEasy

  • Hire me
  • Privacy Policy
  • Contact Us
  • New? Start Here
  • About Us