5 grep tips

Image by Harshad Sharma
Image by Harshad Sharma

Here’s a few tips for using grep.

NOTE : Some of these will work with your version of grep, some may not. In Solaris there can be two or three different versions of grep available. If one of these doesn’t work for you, try reading the man page for your version.

1. Use a pipe to search for multiple items.

shell> grep -P 'text|name' file.txt

2. Use xargs to search in sub-directories.

shell> find . | xargs grep whatever

3. Search for a string starting with a uppercase or lower case s.
shell> grep [Ss]tring

4.  Remove the grep process from the output

shell> ps aux | grep '[p]erl'

5. Finding string with color highlighting

shell> grep --color='auto' -i error /var/log/messages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s