How do I use grep command in Bash?
The grep command searches the given files for lines containing a match to a given pattern list. In other words, use the grep command to search words or strings in a text files. When it finds a match in a file, it will display those line on screen.
grep examples in bash
The syntax is as follows:
grep 'word-to-search' filename egrep 'word1|word2' /path/to/filename
Examples
Search for word vivek in /etc/passwd file, enter:
grep vivek /etc/passwd
Sample outputs:
vivek:x:500:100::/home/vivek:/bin/bash
Search for word vivek or priyanka in /etc/passwd file, enter:
egrep -w 'vivek|priyanka' /etc/passwd
Search all files in directory /nas/project for words 'main':
egrep -R 'main' *
OR limit search to word only
egrep -Rw 'main' *
Highlight words with color:
egrep --color -R 'main' *
Recommend readings:
- How To Use grep Command In Linux / UNIX
- Search Multiple Words / String Pattern Using grep Command
- Quick Shell Tip: Remove grep command while grepping something using ps command
- Search Linux / UNIX log files smartly for an alert or warning error
- See all grep command examples in our blog or FAQ section.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop





![HowTo: Use grep Command In Linux / UNIX [ Examples ]](http://s13.cyberciti.org/images/shared/rp/3/25.jpg)






{ 0 comments… add one now }