grep command options

Disclaimer: Accessing the information on this page means you agree to the Sites Privacy Policy & Terms of Use Agreement.


The grep is the Unix utility that filter searches a file for a particular pattern of characters and displays all lines that contain that pattern.

Grep stands for globally search for regular expression and prints out
Syntax:

Options

Command-line options switches of grep:

  • -e pattern expression with this option
  • -i: Ignore uppercase vs. lowercase, can say the case for matching
  • -v: Invert match means prints out all the lines that do not match the pattern
  • -c: Prints output count of matching lines only.
  • -l: Output matching files (list of filenames) only.
  • -n: Prints matching line with a line number.
  • -b: A historical curiosity: Prints each matching line with a block number.
  • -h: Output matching lines without showing them by file names.
  • -s: Output status only.
  • -f file: Take regexes from a file.
  • -w: Match whole word
  • -o: To display only the matched pattern

Command-line options switches of GNU grep :

  • –help
  • -V, –version
  • –regexp=pattern, in addition to -e pattern
  • –invert-match, in addition to -v
  • –word-regexp, in addition to -w
  • –line-regexp, in addition to -x
  • -A num, –after-context=num
  • -B num, –before-context=num
  • -C num, -num, –context=num

Syntax :
grep [options] pattern [files]

Sample Commands

$ ls 
Site.sh cron.txt openssl-1.0 xyz abc unix.txt
$ ls |grep cron
cron.txt

On the contrary, if you want to filter a list unless some entries, put it in the parameter -v:

$ ls | grep -v

Site.sh
cron.txt
openssl-1.0
xyz
abc
unix.txt

Consider the below :

$cat > unix.txt

unix is opensource. unix is great os. unix is great os.
unix is a multiuser os.
Unix linux which one you choose. learn operating system. uNix is easy to learn. Learn unix .unix is a powerful.

Option : -i (Ignore uppercase vs. lowercase, can say the case for matching)

$grep -i "UNix" unix.txt

Output:

unix is opensource. unix is great os. unix is great os.
unix is a multiuser os.
Unix linux which one you choose. learn operating system. uNix is easy to learn. Learn unix .unix is a powerful.

Option: -c (Prints output count of matching lines only)

$grep -c "unix" unix.txt

Output:

3

Option: -l (Output matching files (list of filenames) only.

grep -l "unix" *

Output :

unix.txt

Option : -w (Match whole word)

grep -w "unix" unix.txt

Output :

unix is opensource. unix is great os. unix is great os.
unix is a multiuser os.
Unix linux which one you choose. learn operating system. uNix is easy to learn. Learn unix .unix is a powerful.

Option : -o (To display only the matched pattern)

grep -o "unix" unix.txt

Output :

unix
unix
unix
unix
unix
unix

Option : -n (Prints matching line with a line number)

grep -o "unix" unix.txt

Output :

1:unix is opensource. unix is great os. unix is great os.
2:unix is a multiuser os.
3:Unix linux which one you choose. learn operating system. uNix is easy to learn. Learn unix .unix is a powerful.

This article is contributed by RootLinuxBlog. If you like RootLinuxBlog and would like to contribute, you can submit an article using contact us from. See your article appearing on the RootLinuxBlog main page and help other Techies.
Please write comments if you found any error with the above article would really help us to serve you a better way. Thank you…

Please follow and like us:
grep command options

Leave a Reply

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

Enjoy this blog? Please spread the word :)

Facebook106
Facebook
LINKEDIN98