Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
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
2  
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. –  Maksim May 28 '13 at 19:25
1  
@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
1  
@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
2  
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
show 2 more comments

3 Answers

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
aaaaaaaa
aabbccaaaacccccccccb
bbbbbb
$ grep -oP 'a.*?b' a.txt
aab
aaaacccccccccb

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
[a-z]*.*[AHG].*
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
add comment

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 .*? –  Maksim May 28 '13 at 19:26
add comment

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 –  Maksim May 28 '13 at 19:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.