Get Your grep-fu On

More sysadmin goodness from damonparker.org.

Search for red OR green:

grep ‘red|green’ files

Search for searchtext at the beginning of a line in files:

grep ‘^searchtext’ files

Search for searchtext at the end of a line in files:

grep ‘searchtext$’ files

Search files for blank lines:

grep ‘^$’ files

Search files for US formatted phone numbers (###-###-####):

grep ‘[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]’ files

or:

grep ‘[0-9]{3}-[0-9]{3}-[0-9]{4}’ files

Search for e-commerce or ecommerce in files:

grep e-*commerce files

Search for searchtext case-insenstively in files:

grep -i searchtext files

Chain two grep commands together for more advanced searches. Search for lines in files that contain both partial_name and function:

grep partial_name files | grep function

That one is great for searching source directories for a function definition when you can’t remember the completely function name.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s