Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
grep . filename.txt 

This just returns all of the contents in filename.txt, because . is a regular expression that stands for any character. How do I use grep to search for periods, or other regular expressions, like *?

share|improve this question
Have you tried escaping them with \, as in \. or \*? – Karan May 28 '13 at 19:23
Doesn't work. Dealing with escape characters is something the shell does for you, not the programs themselves. Grep disables most shell preprocessing on the inputs, at least in ubuntu. – MYV May 28 '13 at 19:25
@Maksim Your shell treats the contents inside of single-quotes as literals. This means that the grep command is actually passed \. as the parameter, which it can then interpret as a literal .. If you don't have the \ , then grep simply gets . as the parameter, which matches any character. If you don't have the quotes, then your shell will process the \ , and again grep will simply get . as the parameter. – Darth Android May 28 '13 at 19:35
@Maksim Exactly! This behavior is documented in the manuals for grep and the shell, which is why people were getting a little short from the "lack of research". – Darth Android May 28 '13 at 19:39
The question, as it is currently stated (and answered), has merit. How to search for a string literal that represents a regular expression is a very valid question. Everyone needs to calm down please. – Oliver Salzburg May 28 '13 at 19:43
up vote 10 down vote accepted

grep uses regular expressions by default, any pattern given to grep is assumed to be a regular expression unless you use the -F switch.

Expressions such as *? are not part of the POSIX regex language which is what grep uses by default. You can use such constructs by specifying a different regex language, Perl for example:

$ cat a.txt
$ grep -oP 'a.*?b' a.txt

To search for a line containing at least one period, use

$ grep '\.*' foo.txt

For a line containing only periods, use

$ grep '^\.*$' foo.txt

For more complex regexes, use -F:

$ grep -F '[a-z]*.*[AHG].*' foo.txt
share|improve this answer
your last expression matches lines with only periods AND empty lines. – tink May 28 '13 at 20:48
@tink, thanks, corrected. – terdon May 28 '13 at 20:49

Use fgrep instead if you want to search for literals, not regular expressions.

If you'd merely like to turn off the special meaning of one of the regex characters with grep, simply escape it with the backslash, taking note that if you're typing this at a command line, you may need to "escape the escapes" because your shell may strip out one layer of them before passing them to the child.

share|improve this answer
what if I want to mix and match, like search for a line of periods using .*? – MYV May 28 '13 at 19:26

grep -F will not expand regexes. man grep:

-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

Please notice F and not f!

share|improve this answer
What if I want to mix and match – MYV May 28 '13 at 19:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .