2
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 *?
2
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 *?
10
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].*
your last expression matches lines with only periods AND empty lines. – tink – 2013-05-28T20:48:16.267
5
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.
what if I want to mix and match, like search for a line of periods using .*? – MYV – 2013-05-28T19:26:38.843
4
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
!
What if I want to mix and match – MYV – 2013-05-28T19:33:56.323
2Have you tried escaping them with , as in . or \*? – Karan – 2013-05-28T19:23:36.827
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 – 2013-05-28T19:25:55.573
I don't understand why '.' filename.txt works. It seems to me that wither '.' or . should work, and I dont see why it works when theyre both used at once. Care to explain? – MYV – 2013-05-28T19:31:38.033
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\
, thengrep
simply gets.
as the parameter, which matches any character. If you don't have the quotes, then your shell will process the\
, and againgrep
will simply get.
as the parameter. – Darth Android – 2013-05-28T19:35:37.050Ooh, I see. So grep uses \ to denote that something is a literal, but so does the shell. So I need to wrap '.' in quotes - if I pass in just ., the shell will preprocess away the backslash, and grep just sees ., but if I do '.', grep sees . Thanks! – MYV – 2013-05-28T19:37:56.650
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 – 2013-05-28T19:39:32.413
2The 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. – Der Hochstapler – 2013-05-28T19:43:14.557