Searching a group of files for a text string

How to search a group of files for a text string. …

To search for the string "foobar" (case insensitive) in all XML files in the working directory or any subdirectories, the form is:

$ egrep -rni foobar *.xml

The -r option recurses through any subdirectories. The -n option prints the names of the files with a match, the line number(s) of the matches in each file, and the lines containing the matches. The -i option ignores case in the pattern to be searched for. "*.xml" describes the files to be searched; use an asterisk to search all files.

To search for any of the strings "foo", "bar", and/or "baz" in all files:

$ egrep -rni '(foo|bar|baz)' *

If the files being searched have long lines, you might want to disable the printing of of the matching lines. To do this, substitute -l for -n above, which will print the names of the files with a match without printing lines.

If some of the files may be compressed, use zgrep. Unfortunately zgrep does not support recursion, so don't use the -r option with it.

man egrep


About Warren Post

So far: Customer support guy, jungle guide, IT consultant, beach bum, entrepreneur, teacher, diplomat, over-enthusiastic cyclist. Tomorrow: who knows?
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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