SlideShare une entreprise Scribd logo
1  sur  4
GREP command is one of the most frequently used UNIX command stands for "Global
Regular Expression Print” .GREP command in Unix operating system e.g. Linux,
Solaris, BSD, Ubuntu or IBM AIX is used to search files with matching patterns, by
using grep command in Unix you can search a file which contains a particular word or
particular pattern.


Example 1
Finding relevant word and exclusion of irrelevant word is the most feature of grep
command. Most of the time I look for Exception and Errors in log files and some time I
know certain Exception I can ignore so I use grep -v option to exclude those Exceptions.


grep Exception logfile.txt | grep –v ERROR


This grep command example will search for word "Exception" in logfile.txt and print
them but since we have piped out of first grep command to second grep command
which will exclude all lines which match world "ERROR". To make this grep example
more concrete let's see another example, here we have a file which contains three lines
as shown below:

$ cat example.txt
UNIX operating system
UNIX and Linux operating system
Linux operating system


Now we want to search all lines in file example.txt which contains word UNIX but same
time doesn't contain world Linux.

$ grep UNIX example.txt
UNIX operating system
UNIX and Linux operating system


Now to exclude all lines which contains Linux we will apply another grep command in
this output with option -v to exclude matching word as shown in below grep command.


$ grep UNIX example.txt | grep-v Linux
UNIX operating system
Example 2


If you want to count of a particular word in log file you can use grep -c option to count
the word. Below example of command will print how many times word "Error" has
appeared in logfile.txt.


grep -c "Error" logfile.txt


If we apply this grep command on our example file to find how many lines contains word
e.g. UNIX has occurred in the file :
$ grep -c UNIX example.txt
2


Example 3


Sometime we are not just interested on matching line but also on lines around matching
lines particularly useful to see what happens before any Error or Exception. grep --
context option allows us to print lines around matching pattern. Below example of grep
command in UNIX will print 6 lines around matching line of word "successful" in
logfile.txt


grep --context=6 successful logfile.txt
Show additional six lines after matching very useful to see what is around and to print
whole message if it splits around multiple lines. You can also use command line option
"C" instead of "--context" for example


grep -C 2 'hello'
Prints two lines of context around each matching line.


Example 4


egrep stands for extended grep and it is more powerful than grep command in Unix and
allows more regular exception like you can use "|" option to search for either Error or
Exception by executing just one command.

egrep 'Error|Exception' logfile.txt
Example 5


If you want to do case insensitive search than use -i option from grep command in
UNIX. Grep -i will find occurrence of both Error, error and ERROR and quite useful to
display any sort of Error from log file.


grep -i Error logfile


Example 6


Use grep -w command in UNIX if you find whole word instead of just pattern.
grep -w ERROR logfile


Above grep command in UNIX searches only for instances of 'ERROR' that are entire
words; it does not match `SysERROR'.


For more control, use `<' and `>' to match the start and end of words. For example:


grep 'ERROR>' *


Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.


Example 7


Another useful grep command line option is "grep -l" which display only the file names
which matches the given pattern. Below command will only display file names which
have ERROR?


grep -l ERROR *.log
grep -l 'main' *.java will list the names of all Java files in the current directory whose
contents mention `main'.
Example 8


If you want to see line number of matching lines you can use option "grep -n" below
command will show on which lines Error has appeared.
grep -n ERROR log file.

Contenu connexe

Tendances

Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheetMartin Cabrera
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in GrepDan Morrill
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expressionGagan019
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHPAndrew Kandels
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and RegexJayant Parida
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Perl6 signatures, types and multicall
Perl6 signatures, types and multicallPerl6 signatures, types and multicall
Perl6 signatures, types and multicallSimon Proctor
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)FrescatiStory
 

Tendances (18)

Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
Using Regular Expressions in Grep
Using Regular Expressions in GrepUsing Regular Expressions in Grep
Using Regular Expressions in Grep
 
Unix
UnixUnix
Unix
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHP
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and Regex
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Perl6 signatures, types and multicall
Perl6 signatures, types and multicallPerl6 signatures, types and multicall
Perl6 signatures, types and multicall
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)
 

En vedette

Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 
7th sem it_CSVTU
7th sem it_CSVTU7th sem it_CSVTU
7th sem it_CSVTUnksharma128
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab FileKandarp Tiwari
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commandsaaj_sarkar06
 
Hill-climbing #2
Hill-climbing #2Hill-climbing #2
Hill-climbing #2Mohamed Gad
 
Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacksshekarkcb
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionMohamed Gad
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}FellowBuddy.com
 
Presentation1 linux os
Presentation1 linux osPresentation1 linux os
Presentation1 linux osjoycoronado
 
Basic command ppt
Basic command pptBasic command ppt
Basic command pptRohit Kumar
 
Linux ppt
Linux pptLinux ppt
Linux pptlincy21
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duoJoshua Thijssen
 

En vedette (20)

Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 
Linux basic commands tutorial
Linux basic commands tutorialLinux basic commands tutorial
Linux basic commands tutorial
 
Unix
UnixUnix
Unix
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
7th sem it_CSVTU
7th sem it_CSVTU7th sem it_CSVTU
7th sem it_CSVTU
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Some basic unix commands
Some basic unix commandsSome basic unix commands
Some basic unix commands
 
Hill-climbing #2
Hill-climbing #2Hill-climbing #2
Hill-climbing #2
 
Linux 101-hacks
Linux 101-hacksLinux 101-hacks
Linux 101-hacks
 
Hillclimbing search algorthim #introduction
Hillclimbing search algorthim #introductionHillclimbing search algorthim #introduction
Hillclimbing search algorthim #introduction
 
Chapter 2 (final)
Chapter 2 (final)Chapter 2 (final)
Chapter 2 (final)
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Presentation1 linux os
Presentation1 linux osPresentation1 linux os
Presentation1 linux os
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix pipingLinux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
 
Basic command ppt
Basic command pptBasic command ppt
Basic command ppt
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duo
 

Similaire à Learning Grep

Practical Example of grep command in unix
Practical Example of grep command in unixPractical Example of grep command in unix
Practical Example of grep command in unixJavin Paul
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linuxTeja Bheemanapally
 
15 practical grep command examples in linux : unix
15 practical grep command examples in linux : unix15 practical grep command examples in linux : unix
15 practical grep command examples in linux : unixchinkshady
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfSaravana Kumar
 
Doscommands
DoscommandsDoscommands
DoscommandsDurgule
 
50 most frequently used unix linux commands (with examples)
50 most frequently used unix   linux commands (with examples)50 most frequently used unix   linux commands (with examples)
50 most frequently used unix linux commands (with examples)Rodrigo Maia
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressionsAcácio Oliveira
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.pptGowthamRaju15
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File ConceptsANUSUYA S
 

Similaire à Learning Grep (20)

Practical Example of grep command in unix
Practical Example of grep command in unixPractical Example of grep command in unix
Practical Example of grep command in unix
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
15 practical grep command examples in linux : unix
15 practical grep command examples in linux : unix15 practical grep command examples in linux : unix
15 practical grep command examples in linux : unix
 
Makefiles Bioinfo
Makefiles BioinfoMakefiles Bioinfo
Makefiles Bioinfo
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
 
Doscommands
DoscommandsDoscommands
Doscommands
 
Doscommands
DoscommandsDoscommands
Doscommands
 
Linux intermediate level
Linux intermediate levelLinux intermediate level
Linux intermediate level
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
Bioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-filesBioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-files
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
50 most frequently used unix linux commands (with examples)
50 most frequently used unix   linux commands (with examples)50 most frequently used unix   linux commands (with examples)
50 most frequently used unix linux commands (with examples)
 
Unix training session 3
Unix training   session 3Unix training   session 3
Unix training session 3
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
MCLS 45 Lab Manual
MCLS 45 Lab ManualMCLS 45 Lab Manual
MCLS 45 Lab Manual
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 

Learning Grep

  • 1. GREP command is one of the most frequently used UNIX command stands for "Global Regular Expression Print” .GREP command in Unix operating system e.g. Linux, Solaris, BSD, Ubuntu or IBM AIX is used to search files with matching patterns, by using grep command in Unix you can search a file which contains a particular word or particular pattern. Example 1 Finding relevant word and exclusion of irrelevant word is the most feature of grep command. Most of the time I look for Exception and Errors in log files and some time I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions. grep Exception logfile.txt | grep –v ERROR This grep command example will search for word "Exception" in logfile.txt and print them but since we have piped out of first grep command to second grep command which will exclude all lines which match world "ERROR". To make this grep example more concrete let's see another example, here we have a file which contains three lines as shown below: $ cat example.txt UNIX operating system UNIX and Linux operating system Linux operating system Now we want to search all lines in file example.txt which contains word UNIX but same time doesn't contain world Linux. $ grep UNIX example.txt UNIX operating system UNIX and Linux operating system Now to exclude all lines which contains Linux we will apply another grep command in this output with option -v to exclude matching word as shown in below grep command. $ grep UNIX example.txt | grep-v Linux UNIX operating system
  • 2. Example 2 If you want to count of a particular word in log file you can use grep -c option to count the word. Below example of command will print how many times word "Error" has appeared in logfile.txt. grep -c "Error" logfile.txt If we apply this grep command on our example file to find how many lines contains word e.g. UNIX has occurred in the file : $ grep -c UNIX example.txt 2 Example 3 Sometime we are not just interested on matching line but also on lines around matching lines particularly useful to see what happens before any Error or Exception. grep -- context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt grep --context=6 successful logfile.txt Show additional six lines after matching very useful to see what is around and to print whole message if it splits around multiple lines. You can also use command line option "C" instead of "--context" for example grep -C 2 'hello' Prints two lines of context around each matching line. Example 4 egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception like you can use "|" option to search for either Error or Exception by executing just one command. egrep 'Error|Exception' logfile.txt
  • 3. Example 5 If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR and quite useful to display any sort of Error from log file. grep -i Error logfile Example 6 Use grep -w command in UNIX if you find whole word instead of just pattern. grep -w ERROR logfile Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'. For more control, use `<' and `>' to match the start and end of words. For example: grep 'ERROR>' * Searches only for words ending in 'ERROR', so it matches the word `SysERROR'. Example 7 Another useful grep command line option is "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR? grep -l ERROR *.log grep -l 'main' *.java will list the names of all Java files in the current directory whose contents mention `main'.
  • 4. Example 8 If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared. grep -n ERROR log file.