Linux Grep Tips

by damonp on November 17, 2010

in General

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
Search for searchtext at the beginning of a line in files:

grep '^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.

Popularity: 3%

Most Popular Posts

Damon Parker is a freelance sysadmin and web developer in Texas. He specializes in server setup, server security and high performance server configurations. Need help setting up a web server or getting a server back online after a crash or hack? Email Damon

Leave a Comment

Previous post:

Next post: