Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep not as a regular expression

Tags:

linux

grep

I need to search for a PHP variable $someVar. However, Grep thinks that I am trying to run a regex and is complaining:

$ grep -ir "Something Here" * | grep $someVar Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. $ grep -ir "Something Here" * | grep "$someVar" <<Here it returns all rows with "someVar", not only those with "$someVar">> 

I don't see an option for telling grep not to interpret the string as a regex, but to include the $ as just another string character.

like image 486
dotancohen Avatar asked Feb 23 '12 15:02

dotancohen


People also ask

Is grep a regular expression?

grep is one of the most useful and powerful commands in Linux for text processing. grep searches one or more input files for lines that match a regular expression and writes each matching line to standard output.

How do you grep except?

Exclude Words and Patterns To display only the lines that do not match a search pattern, use the -v ( or --invert-match ) option. The -w option tells grep to return only those lines where the specified string is a whole word (enclosed by non-word characters). By default, grep is case-sensitive.

What type of regex does grep use?

Grep is an implementation of POSIX regular expressions. There are two types of posix regular expressions -- basic regular expressions and extended regular expressions. In grep, generally you use the -E option to allow extended regular expressions.


2 Answers

Use fgrep (deprecated), grep -F or grep --fixed-strings, to make it treat the pattern as a list of fixed strings, instead of a regex.

For reference, the documentation mentions (excerpts):

-F --fixed-strings Interpret the pattern as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. (-F is specified by POSIX.)

fgrep is the same as grep -F. Direct invocation as fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

For the complete reference, check: https://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html

like image 64
Hasturkun Avatar answered Sep 19 '22 17:09

Hasturkun


grep -F is a standard way to tell grep to interpret argument as a fixed string, not a pattern.

like image 39
rkhayrov Avatar answered Sep 20 '22 17:09

rkhayrov