Escaping * with Regular Expressions and Grep
You can also achieve the same thing by instructing grep
that the string it's to match is a fixed string. The switch to do this is -F
or --fixed-strings
.
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
So something like this will do it:
$ grep -F "**" somefile.txt
Example
$ cat somefile.txt
** blah
blahblah
** hi
Grepping the file produces this:
$ grep -F "**" somefile.txt
** blah
** hi
So try :
egrep "^\*\*" YOUR_FILE
Don't forget to use double quote.
Note: Use egrep
instead of grep
.
If you want to use grep
use grep -E
In:
grep \*\* fileName
The backslashes are used to escape the *
in the shell (where *
is a globbing operator).
What grep
receives as its second argument is a string of two characters: **
.
As a regular expression, that means any (0 or more) number of star characters, so basically it matches everywhere since it also matches the empty string which explains why you get all the lines of the file.
Because *
is special to grep
regex as well, you need to escape it there as well. Best is to use single quotes instead of backslashes to escape *
to the shell (because single quotes are strong shell quotes that escape every character but the single quote character itself), and use backslashes to escape *
to grep. Double quotes would work as well in that instance, but beware that backslashes are still special to the shell inside double quotes.
So:
grep '\*\*' somefile.txt
(with *
escaped so they're no longer regex operators but considered as literal characters) would return the lines of somefile.txt
that contain a sequence of 2 star characters. If you want them to be found only at the beginning of the line, you've got to use the anchoring regex operator ^
:
grep '^\*\*' somefile.txt
An alternative way for *
not to be taken as a regex operator would be to use character ranges:
grep '^[*][*]' somefile.txt
An alternative way to specify two star characters is to write it:
grep '^\*\{2\}' somefile.txt
(where \{
is another regex operator) which is easier to read if you use extended regular expressions as when passing the -E
option to grep
(avoid egrep
as it's not standard):
grep -E '^\*{2}' somefile.txt
(in extended regular expressions, {
is the regex operator).