1

I'd like to search for an html string which contains characters interpreted by grep as special characters. Is there a flag or command which will tell grep to interpret all characters in the search string as literal characters, NOT as special characters?

Perfect world:

grep --ignorespecial '<a href="/abc/def" class="foo.bar"/>'
starsinmypockets
  • 2,264
  • 4
  • 34
  • 45
  • possible duplicate of [Grep for special characters in Unix](http://stackoverflow.com/questions/12387685/grep-for-special-characters-in-unix) – devnull Jun 10 '14 at 16:01
  • Can you clarify? "search for a html string which contains multiple special characters" and "tell grep to ignore all special characters and search for the 'literal' string?" seem to contradict each other – Martin Konecny Jun 10 '14 at 16:07
  • 1
    OK Go easy on me people - I did find the answer I was looking for for which I'm grateful... I want to search the term inside the quotes, ignoring all special characters. Am I missing something? Is there a more clear way to ask the question? Is this a 'stupid question'? – starsinmypockets Jun 10 '14 at 16:26
  • 2
    The phrase "ignoring all special characters" is unclear. I think you mean that searching for `foo*bar` should match `foo*bar`, but it could mean that `foo*bar` should match `foobar`. Rather than *ignoring* special characters, I think you mean to say that you want to treat them as non-special characters. (Which is exactly what `fgrep` does.) – Keith Thompson Jun 10 '14 at 16:38

1 Answers1

2

Yes! Use grep -F.

From man grep:

-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.)

Test

$ cat a
hello
<a href="/abc/def" class="foo.bar"/>my href</a>

<a href="/abc/def/ghi" class="foo.bar"/>

$ grep -F '<a href="/abc/def" class="foo.bar"/>' a
<a href="/abc/def" class="foo.bar"/>my href</a>

Or with more bash codes:

$ cat a
hello
<a href="/abc/def" class="foo.bar"/>my href</a>

This is/ a $line with some ^codes * yeah

$ grep 'This is/ a $line with some ^codes * yeah' a   # no -F, no matches
$

$ grep -F 'This is/ a $line with some ^codes * yeah' a # -F, matches
This is/ a $line with some ^codes * yeah
fedorqui
  • 275,237
  • 103
  • 548
  • 598