SlideShare a Scribd company logo
4
Most read
5
Most read
12
Most read
How to use Grep command in Unix or Linux
Grep command in Unix This tutorial is about how to use grep command in unix and here we will see some practical example of using grep in Unix "grep"  one of the most frequently used UNIX command stands for "Global Regular Expression Print". This grep command tutorial is not about theory of  UNIX grep  but to practical use of grep in UNIX and here I am sharing my experience on use of  grep command in Linux  with an aim that this would serve as quick guide or tutorial for using grep in UNIX for new beginners and help them to understand the grep command better and its thoughtful usage in UNIX or Linux.
How people use Grep in Unix Many people use grep just for finding words in a file and missed the real potential of grep by not using all its powerful command line options and its regular expression capability which could not only save a lot of time but also works as a great and powerful tool while analyzing large set of data or log files.  Also  find command in UNIX  can be used in place of grep at many places.
10 examples of grep command in UNIX and Linux Following  examples on grep command in UNIX  are based on my experience and I use them on daily basis in my work. These examples are by no means complete so please contribute your grep command tips or how you are using grep in Linux to make it more useful and allow all of us to benefit from each others experience and work efficiently in UNIX or Linux. So here we go………….
Finding matching word and excluding unwanted  1) Finding relevant word and exclusion irrelevant word. 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
Counting number of matching word 2) If you want to count of a particular word in log file you can use grep -c option to count the word. Below command will print how many times word "Error" has appeared in logfile.txt grep -c "Error" logfile.txt
Displaying Context around matching word 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.
Using EGrep for sophisticated search 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.  grep -i Error logfile
Using Zgrep to search in gzip file 6)  zgrep  is another great version of grep command in Unix which is used to perform same operation as grep does but with .gz files. Many a times we gzip the old file to reduce size and later wants to look or find something on those file. zgrep is your man for those days. Below command will print all files which have "Error" on them. zgrep -i Error *.gz
Finding whole word using grep in Unix Linux 7) 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'.
Display only file names of matching String 8) Another useful grep command line option is &quot;grep -l&quot; 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'.
Displaying line number of matches using grep in unix 9) If you want to see line number of matching lines you can use option &quot;grep -n&quot; below command will show on which lines Error has appeared. grep -n ERROR log file.
Recursive search using grep in Unix 10) If you want to do recursive search using grep command in Unix there are two options either use &quot;-R&quot; command line option or increase directory one by one as shown below. Grep –R HelloWorld * Or Grep HelloWorld */* Grep HelloWorld */*/* Everytime you put an aditional * it will search one level down.
Now I have two bonus examples of grep command in unix: 11)  grep command in UNIX  can show matching patter in color which is quite useful to highlight the matching section , to see matching pattern in color use below command. grep Exception  today.log --color 12) at last there are three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep‘ use it based on your need.
Summary These  examples of grep command in UNIX  are something which I use on daily basis; I have seen more sophisticated use of grep with regular expression. I will list some more examples of grep command in UNIX as I come across and find useful to share. As per my experience having good hold on grep and  UNIX find command  with knowledge of regular expression will be great for you day to day life if you need to look log files or config files or need to do production support on electronic trading systems or any other kind of system which is running on UNIX. This list of grep command in UNIX is by no means complete and I look forward from you guys to share how you are using grep command in UNIX.
Author Name: Javin Paul Website:  http://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.html Javin Paul is an expert in the area of Java, Unix and TIBCO RV and  and has been working with these technology from past 7 years while working in various project in finance and trading domain.
Ad

Recommended

15 practical grep command examples in linux : unix
15 practical grep command examples in linux : unix
chinkshady
 
15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
Giovanni Marco Dall'Olio
 
Unix And C
Unix And C
Dr.Ravi
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1
Dr.Ravi
 
Using Unix
Using Unix
Dr.Ravi
 
Airlover 20030324 1
Airlover 20030324 1
Dr.Ravi
 
Learning sed and awk
Learning sed and awk
Yogesh Sawant
 
Linux intro 4 awk + makefile
Linux intro 4 awk + makefile
Giovanni Marco Dall'Olio
 
Talk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Unix Tutorial
Unix Tutorial
Sanjay Saluth
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions
Acácio Oliveira
 
Hex file and regex cheat sheet
Hex file and regex cheat sheet
Martin Cabrera
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
Tristan Penman
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
The best unix shell scripting interview questions 2018 learn now!
The best unix shell scripting interview questions 2018 learn now!
mia avery
 
cs3157-summer06-lab1
cs3157-summer06-lab1
tutorialsruby
 
Linux intro 2 basic terminal
Linux intro 2 basic terminal
Giovanni Marco Dall'Olio
 
Vim and Python
Vim and Python
majmcdonald
 
Unit 6
Unit 6
siddr
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Phyton Learning extracts
Phyton Learning extracts
Pavan Babu .G
 
Introduction to Clime
Introduction to Clime
Mosky Liu
 
Qt Translations
Qt Translations
Jussi Pohjolainen
 
class12_Networking2
class12_Networking2
T. J. Saotome
 
Web Application Security: Introduction to common classes of security flaws an...
Web Application Security: Introduction to common classes of security flaws an...
Thoughtworks
 
Defeating The Network Security Infrastructure V1.0
Defeating The Network Security Infrastructure V1.0
Philippe Bogaerts
 
Practical unix utilities for text processing
Practical unix utilities for text processing
Anton Arhipov
 

More Related Content

What's hot (18)

Linux intro 4 awk + makefile
Linux intro 4 awk + makefile
Giovanni Marco Dall'Olio
 
Talk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Unix Tutorial
Unix Tutorial
Sanjay Saluth
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions
Acácio Oliveira
 
Hex file and regex cheat sheet
Hex file and regex cheat sheet
Martin Cabrera
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
Tristan Penman
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
The best unix shell scripting interview questions 2018 learn now!
The best unix shell scripting interview questions 2018 learn now!
mia avery
 
cs3157-summer06-lab1
cs3157-summer06-lab1
tutorialsruby
 
Linux intro 2 basic terminal
Linux intro 2 basic terminal
Giovanni Marco Dall'Olio
 
Vim and Python
Vim and Python
majmcdonald
 
Unit 6
Unit 6
siddr
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Phyton Learning extracts
Phyton Learning extracts
Pavan Babu .G
 
Introduction to Clime
Introduction to Clime
Mosky Liu
 
Qt Translations
Qt Translations
Jussi Pohjolainen
 
Talk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions
Acácio Oliveira
 
Hex file and regex cheat sheet
Hex file and regex cheat sheet
Martin Cabrera
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
Tristan Penman
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
Acácio Oliveira
 
The best unix shell scripting interview questions 2018 learn now!
The best unix shell scripting interview questions 2018 learn now!
mia avery
 
cs3157-summer06-lab1
cs3157-summer06-lab1
tutorialsruby
 
Unit 6
Unit 6
siddr
 
Phyton Learning extracts
Phyton Learning extracts
Pavan Babu .G
 
Introduction to Clime
Introduction to Clime
Mosky Liu
 

Viewers also liked (20)

class12_Networking2
class12_Networking2
T. J. Saotome
 
Web Application Security: Introduction to common classes of security flaws an...
Web Application Security: Introduction to common classes of security flaws an...
Thoughtworks
 
Defeating The Network Security Infrastructure V1.0
Defeating The Network Security Infrastructure V1.0
Philippe Bogaerts
 
Practical unix utilities for text processing
Practical unix utilities for text processing
Anton Arhipov
 
PHP Secure Programming
PHP Secure Programming
Balavignesh Kasinathan
 
Secure shell protocol
Secure shell protocol
Baspally Sai Anirudh
 
Sed & awk the dynamic duo
Sed & awk the dynamic duo
Joshua Thijssen
 
Unix command-line tools
Unix command-line tools
Eric Wilson
 
Web Application Security with PHP
Web Application Security with PHP
jikbal
 
How to Setup A Pen test Lab and How to Play CTF
How to Setup A Pen test Lab and How to Play CTF
n|u - The Open Security Community
 
Unix Command Line Productivity Tips
Unix Command Line Productivity Tips
Keith Bennett
 
Secure SHell
Secure SHell
Çağrı Çakır
 
Secure Shell(ssh)
Secure Shell(ssh)
Pina Parmar
 
SSH
SSH
Zach Dennis
 
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Michael Coates
 
SSH - Secure Shell
SSH - Secure Shell
Peter R. Egli
 
Top 100 Linux Interview Questions and Answers 2014
Top 100 Linux Interview Questions and Answers 2014
iimjobs and hirist
 
RHCE FINAL Questions and Answers
RHCE FINAL Questions and Answers
Radien software
 
Introduction to SSH
Introduction to SSH
Hemant Shah
 
Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg
 
Web Application Security: Introduction to common classes of security flaws an...
Web Application Security: Introduction to common classes of security flaws an...
Thoughtworks
 
Defeating The Network Security Infrastructure V1.0
Defeating The Network Security Infrastructure V1.0
Philippe Bogaerts
 
Practical unix utilities for text processing
Practical unix utilities for text processing
Anton Arhipov
 
Sed & awk the dynamic duo
Sed & awk the dynamic duo
Joshua Thijssen
 
Unix command-line tools
Unix command-line tools
Eric Wilson
 
Web Application Security with PHP
Web Application Security with PHP
jikbal
 
Unix Command Line Productivity Tips
Unix Command Line Productivity Tips
Keith Bennett
 
Secure Shell(ssh)
Secure Shell(ssh)
Pina Parmar
 
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Virtual Security Lab Setup - OWASP Broken Web Apps, Webgoat, & ZAP
Michael Coates
 
Top 100 Linux Interview Questions and Answers 2014
Top 100 Linux Interview Questions and Answers 2014
iimjobs and hirist
 
RHCE FINAL Questions and Answers
RHCE FINAL Questions and Answers
Radien software
 
Introduction to SSH
Introduction to SSH
Hemant Shah
 
Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg
 
Ad

Similar to Practical Example of grep command in unix (20)

Learning Grep
Learning Grep
Vikas Kumar CSM®
 
Grep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
Unix
Unix
lilututu
 
grep and egrep linux presentation for lecture
grep and egrep linux presentation for lecture
pigog73521
 
15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
grep.1.pdf
grep.1.pdf
Saravana Kumar
 
grep.1.pdf
grep.1.pdf
Saravana Kumar
 
Grep
Grep
Dr.M.Karthika parthasarathy
 
Spsl II unit
Spsl II unit
Sasidhar Kothuru
 
L4_grep command ppt for unix linux programming
L4_grep command ppt for unix linux programming
Devendra Meena
 
Using Regular Expressions in Grep
Using Regular Expressions in Grep
Dan Morrill
 
Regexp
Regexp
Bhorotsana RK
 
terminal command2.pptx with good explanation
terminal command2.pptx with good explanation
farsankadavandy
 
6
6
satishbb
 
Unit 4 scripting and the shell
Unit 4 scripting and the shell
Bhushan Pawar -Java Trainer
 
L5_regular expression command for linux unix
L5_regular expression command for linux unix
Devendra Meena
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
Saravana Kumar
 
08 text processing_tools
08 text processing_tools
Shay Cohen
 
unix- Sort, uniq,tr,grep
unix- Sort, uniq,tr,grep
Dr. Girish GS
 
Unix lab manual
Unix lab manual
Chaitanya Kn
 
Grep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
grep and egrep linux presentation for lecture
grep and egrep linux presentation for lecture
pigog73521
 
15 practical grep command examples in linux
15 practical grep command examples in linux
Teja Bheemanapally
 
L4_grep command ppt for unix linux programming
L4_grep command ppt for unix linux programming
Devendra Meena
 
Using Regular Expressions in Grep
Using Regular Expressions in Grep
Dan Morrill
 
terminal command2.pptx with good explanation
terminal command2.pptx with good explanation
farsankadavandy
 
L5_regular expression command for linux unix
L5_regular expression command for linux unix
Devendra Meena
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
Saravana Kumar
 
08 text processing_tools
08 text processing_tools
Shay Cohen
 
unix- Sort, uniq,tr,grep
unix- Sort, uniq,tr,grep
Dr. Girish GS
 
Ad

Recently uploaded (20)

Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 

Practical Example of grep command in unix

  • 1. How to use Grep command in Unix or Linux
  • 2. Grep command in Unix This tutorial is about how to use grep command in unix and here we will see some practical example of using grep in Unix &quot;grep&quot; one of the most frequently used UNIX command stands for &quot;Global Regular Expression Print&quot;. This grep command tutorial is not about theory of UNIX grep but to practical use of grep in UNIX and here I am sharing my experience on use of grep command in Linux with an aim that this would serve as quick guide or tutorial for using grep in UNIX for new beginners and help them to understand the grep command better and its thoughtful usage in UNIX or Linux.
  • 3. How people use Grep in Unix Many people use grep just for finding words in a file and missed the real potential of grep by not using all its powerful command line options and its regular expression capability which could not only save a lot of time but also works as a great and powerful tool while analyzing large set of data or log files.  Also find command in UNIX can be used in place of grep at many places.
  • 4. 10 examples of grep command in UNIX and Linux Following examples on grep command in UNIX are based on my experience and I use them on daily basis in my work. These examples are by no means complete so please contribute your grep command tips or how you are using grep in Linux to make it more useful and allow all of us to benefit from each others experience and work efficiently in UNIX or Linux. So here we go………….
  • 5. Finding matching word and excluding unwanted 1) Finding relevant word and exclusion irrelevant word. 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
  • 6. Counting number of matching word 2) If you want to count of a particular word in log file you can use grep -c option to count the word. Below command will print how many times word &quot;Error&quot; has appeared in logfile.txt grep -c &quot;Error&quot; logfile.txt
  • 7. Displaying Context around matching word 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 &quot;successful&quot; 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 &quot;C&quot; instead of &quot;--context&quot; for example grep -C 2 'hello' * Prints two lines of context around each matching line.
  • 8. Using EGrep for sophisticated search 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 &quot;|&quot; option to search for either Error or Exception by executing just one command. grep -i Error logfile
  • 9. Using Zgrep to search in gzip file 6) zgrep is another great version of grep command in Unix which is used to perform same operation as grep does but with .gz files. Many a times we gzip the old file to reduce size and later wants to look or find something on those file. zgrep is your man for those days. Below command will print all files which have &quot;Error&quot; on them. zgrep -i Error *.gz
  • 10. Finding whole word using grep in Unix Linux 7) 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'.
  • 11. Display only file names of matching String 8) Another useful grep command line option is &quot;grep -l&quot; 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'.
  • 12. Displaying line number of matches using grep in unix 9) If you want to see line number of matching lines you can use option &quot;grep -n&quot; below command will show on which lines Error has appeared. grep -n ERROR log file.
  • 13. Recursive search using grep in Unix 10) If you want to do recursive search using grep command in Unix there are two options either use &quot;-R&quot; command line option or increase directory one by one as shown below. Grep –R HelloWorld * Or Grep HelloWorld */* Grep HelloWorld */*/* Everytime you put an aditional * it will search one level down.
  • 14. Now I have two bonus examples of grep command in unix: 11) grep command in UNIX can show matching patter in color which is quite useful to highlight the matching section , to see matching pattern in color use below command. grep Exception  today.log --color 12) at last there are three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep‘ use it based on your need.
  • 15. Summary These examples of grep command in UNIX are something which I use on daily basis; I have seen more sophisticated use of grep with regular expression. I will list some more examples of grep command in UNIX as I come across and find useful to share. As per my experience having good hold on grep and UNIX find command with knowledge of regular expression will be great for you day to day life if you need to look log files or config files or need to do production support on electronic trading systems or any other kind of system which is running on UNIX. This list of grep command in UNIX is by no means complete and I look forward from you guys to share how you are using grep command in UNIX.
  • 16. Author Name: Javin Paul Website: http://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.html Javin Paul is an expert in the area of Java, Unix and TIBCO RV and and has been working with these technology from past 7 years while working in various project in finance and trading domain.