Home » 2014 » December » 3 » GREP command in Linux to search for a pattern

11:48 AM
GREP command in Linux to search for a pattern

grep, egrep, fgrep - print lines matching a pattern given

FORMAT
       grep [options] PATTERN [FILE...]
       grep [options] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       Grep  searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN.
       By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available.  

  • Egrep is the same as grep -E.  
  • Fgrep is the same as grep -F.

WE WILL TRY TO LEARN THIS COMMAND WITH EXAMPLES:

1. Search for a pattern in a file-case sensitive

The below command will search for a pattern "command" in the file "test"

[oracle@jpdba ~]$ grep command test
this is a test file for grep command
Lets try this command on this file

2. Search for a pattern in a file-case insensitive

 -i, --ignore-case

              Ignore case distinctions in both the PATTERN and the input files.

[oracle@jpdba ~]$ grep -i command test
this is a test file for grep command
Lets try this command on this file
COMMAND is in caps


3. Print count of matches with the file name

-c, --count

              Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match option (see below), count  non-matching lines.

[oracle@jpdba ~]$ grep -c test *
demo:1
Desktop:0
oradiag_oracle:0
oraInventory:0
shanky.usb:0
test:1


4. Print count of matches in a file- case insensitive

[oracle@jpdba ~]$ grep -ic command *
demo

:0
Desktop:0
oradiag_oracle:0
oraInventory:0
shanky.usb:0
test:3


 


You can set the color of the matched pattern

 --colour[=WHEN], --color[=WHEN]
              Surround the matching string with the marker find in GREP_COLOR environment variable. WHEN may be "never", "always" or "auto".

 

[oracle@jpdba ~]$ grep --color=auto -i command test
this is a test file for grep command
Lets try this command on this file
COMMAND is in caps

You can set alias of grep so that it will always show the matched pattern in color.

[oracle@jpdba ~]$ alias grep='grep --color=auto'

 

[oracle@jpdba ~]$ grep -i command *
test
[oracle@jpdba ~]$ grep -i command test
this is a test file for grep command
Lets try this command on this file
COMMAND is in caps

 -l, --files-with-matches

              Suppress  normal  output;  instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.


 -n, --line-number
              Prefix each line of output with the line number within its input file.

  -o, --only-matching
              Show only the part of a matching line that matches
PATTERN.

shanky@localhost:/home/shanky/test:> grep This testFile
1. This is s test file
6. This file can be deleted later when

shanky@localhost:/home/shanky/test:> grep -o This testFile
This
This

6. Print the lines other than the matched pattern
   -v, --invert-match
              Invert the sense of matching, to select non-matching lines.

[oracle@jpdba ~]$ grep -v command test
It contains some random texts
that will be used for pattern matching
COMMAND is in caps
cheers

[oracle@jpdba ~]$ grep -iv command test
It contains some random texts
that will be used for pattern matching
cheers

7. Print the line number in a file where the match found

[oracle@jpdba ~]$ grep -in command test
1:this is a test file for grep command
4:Lets try this command on this file
5:COMMAND is in caps


8.We told you earlier that grep -e is same as egrep which is extension of grep. Print the lines which are containing either one or more pattern.

[oracle@jpdba ~]$ grep -E 'is|test|command' test
this is a test file for grep command
Lets try this command on this file
COMMAND is in caps


[oracle@jpdba ~]$ egrep 'is|test\command' test
this is a test file for grep command
Lets try this command on this file
COMMAND is in caps
[oracle@jpdba ~]$

 

The aboce command will print the lines in the file test which matches either 'is' or 'test' or 'command'.

9. Find out and list the file systems which have usage % more than 80.

shan@localhost:> df -k|grep %|tr -s " " ":"|cut -d ":" -f5,6|grep ^[8-9][0-9]%*
82%:/nfs/fs1
82%:/nfs/fs2

  1. The above command used "df -k" to list all the filesystems with there disk usage details,
  2. We have grepped with "%" to print only the lines containing "%".
  3. Next, we have translated the extra spaces to single ":".
  4. We have seperated the columns 5th and 6th, just to get the percentage and the file system.
  5. Then, finally we have used "grep ^[8-9][0-9]%*" which means it will print all lines starting with 8or 9 followed by any one digit followed by % and followed by anything.

 

 
 

Category: Open System-Linux | Views: 1603 | Added by: shanky | Tags: grep, grep command with examples, search for a pattern in linux, grep command in linux, pattern matching in linux | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-10-12][Open System-Linux]
GETENT command in linux to get entries from administrative database
[2016-12-25][Open System-Linux]
Guide for working with zipped and archived files in Linux/AIX
[2014-02-19][Open System-Linux]
Some unique and special commands in Linux
[2015-08-18][Open System-Linux]
How to open a PDF file or a Postscript file in Linux - Evince Document viewer?
[2016-05-24][Open System-Linux]
FACTER command in Linux : showing system facts

Total comments: 0
ComForm">
avatar