Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape a double quote with GNU grep for windows/dos?

Tags:

grep

cmd

gnu

I'm trying to search for

Assembly="Foobar

in all files and direct the output to tmp.txt

I've tried (cmd/error)

grep -r "Assembly=\"Foobar" . >> tmp.txt error (invalid argument)

grep -r "Assembly=""Foobar" . >> tmp.txt
error (not enough space)

grep -r 'Assembly="Foobar' . >> tmp.txt
error (not enough space)

very frustrating. Any suggestions?

like image 704
WhiskerBiscuit Avatar asked Nov 06 '13 15:11

WhiskerBiscuit


People also ask

How do you escape double quotes in DOS?

inside double-quoted strings, use `" or "" to escape double-quotes. inside single-quoted strings, use '' to escape single-quotes.

How do you escape a double quote in grep?

Generally, the backslash (\) character is yet another option to quash the inbuilt shell interpretation and tell the shell to accept the symbol literally.

How do you escape a quote from a string?

Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings. Alternative forms for the last two are '⁠\u{nnnn}⁠' and '⁠\U{nnnnnnnn}⁠'. All except the Unicode escape sequences are also supported when reading character strings by scan and read.


1 Answers

You need to worry about escaping quotes for both grep and cmd.exe

grep expects quotes to be escaped as \"

cmd.exe escapes quotes as ^". The quote is a state machine, meaning unescaped quotes toggle quote semantics on and off. When quoting is on, special characters like <, |, &, etc are treated as literals. The first unescaped quote turns on quote semantics. The next quote turns quote semantics off. It is only possible to escape a quote when quoting is OFF. Unbalanced quotes result in the remainder being quoted. So in your case, the >>tmp.txt is not treated as redirection by cmd.exe, so it gets passed to grep as an argument.

Any of the following will work:

grep -r "Assembly=\"Foobar^" . >>tmp.txt
grep -r ^"Assembly=\"Foobar" . >>tmp.txt
grep -r ^"Assembly=\^"Foobar^" . >>tmp.txt
grep -r Assembly=\^"Foobar . >>tmp.txt

If your search term included spaces, then grep needs the enclosing quotes, so you would need any of the first three forms. The last form would not work with spaces in the search term.

like image 125
dbenham Avatar answered Nov 07 '22 18:11

dbenham