Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep exact literal string (no regex)

Is there a way to grep (or use another command) to find exact strings, using NO regex?

For example, if I want to search for (literally):

/some/file"that/has'lots\of"invalid"chars/and.triggers$(#2)[*~.old][3].html 

I don't want to go through and escape every single "escapable". Essentially, I want to pass it through, like I would with echo:

$ echo "/some/file\"that/has'lots\of\"invalid\"chars/and.triggers$(#2)[*~.old][3].html" /some/file"that/has'lots\of"invalid"chars/and.triggers$(#2)[*~.old][3].html 
like image 873
Nathan J.B. Avatar asked Jan 15 '13 22:01

Nathan J.B.


People also ask

How do you grep for an exact string?

Grep to Match First and Last Character. You can also use the grep command to find an exact match by using the beginning(^) and ending($) character.

How do I search for exact words in grep?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

Which grep option will look for the exact match only?

Exact Match with -w Option The -w option is used to match specified term exactly for the given content. Actually the -w option is created to match words where single word can match.

How do I grep for a fixed string in Linux?

Either use grep -E (the modern form for egrep ) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like . , ? , * , ^ , $ , \ , [ , ( , ) , { , } ) in your fixed strings.


1 Answers

Use fgrep, it's the same as grep -F (matches a fixed string).

like image 164
pldoverflow Avatar answered Sep 25 '22 10:09

pldoverflow