11

How can I properly escape a quote in a search string when using findstr.exe?

Example:

findstr /misc:"namespace=\"" *.cs > ns.txt

This outputs to the console, instead of to the file I specified.

I am doing this directly on the command line, not actually in a batch file, though that information might be useful too.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183

4 Answers4

9

Please correct me if I'm wrong, but I think I've figured it out:

findstr.exe /misc:^"namespace=\^"^" *.cs > ns.txt

This seems to give the correct output, even if you have spaces in your search string. It allows file redirection, piping, and additional literals in the same findstr.exe invocation to work correctly.

The original command in my question doesn't work because both cmd.exe and findstr.exe have special processing for the " character. I ended up with an unmatched set of quotes in cmd.exe's processing.

The new command in my answer works because ^" allows the quote to pass from cmd.exe to findstr.exe, and \" tells findstr.exe to ignore that quote for command processing purposes, and treat it as a character literal.

Edit:

Well, my solution was right, but the reason it is correct was totally wrong. I wrote a small program to test it.

I found out that cmd.exe passes this input to the program when I pass the bad command line:

test.exe /misc:namespace=" *.cs > ns.txt

With the characters escaped correctly, cmd.exe passes this input to the program (and redirects output to a file):

test.exe /misc:namespace=" *.cs
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
5

Found Re: FINDSTR search for a couble quote and redirect/pipe the output

Try this: 

findstr > x.txt /S /I /M /C:"\.\"" * 

I have no idea why this works.

Doesn't work for piping the output though. See the related link piping findstr's output

Community
  • 1
  • 1
typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • That makes it look like you can do file redirection mid-line, and pass arguments afterwards? That kind of upsets my whole world of batch file understanding :) – Merlyn Morgan-Graham Feb 13 '11 at 07:08
  • Yes, redirects are allowed at the front, middle or end of a line, even multiple redirects are allowed, but only the last will become effective. – jeb Feb 13 '11 at 15:43
4

According to my tests the correct escape character is backslash:

c:\Temp>findstr /isc:"session id=\"59620\"" C:\Temp\logs\some*.xml
C:\Temp\logs\some_2016_11_03.xml: <session id="59620" remoteAddress="192.168.195.3:49885"/>
Ciove
  • 366
  • 4
  • 9
  • ding! exactly what i needed ! and didn't know you could specify multiple options with one '/' :), nice – Larphoid Jul 19 '18 at 19:22
2

Wouldn't this be just enough:

findstr /misc:namespace=^" *.cs > ns.txt

?

EDIT

If you were searching for a way to pass the " character inside a quoted parameter, then it could be (using your example)

findstr /misc:"namespace=""" *.cs > ns.txt

(the " character is repeated twice inside a quoted string).

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • For this problem, yes. For a general purpose problem, such as if you have spaces in your search string, no. – Merlyn Morgan-Graham Feb 13 '11 at 22:20
  • @Merlyn Morgan-Graham: Added solution for a more general problem. – Andriy M Feb 13 '11 at 23:14
  • @Andriy: Check out my answer. It correctly handles cases such as this - `findstr /c:"this is ""a test""" blah.txt`, when blah.txt contains only the line `this is "a test"`. Double quoting doesn't solve the problem when spaces are involved. – Merlyn Morgan-Graham Feb 14 '11 at 06:25
  • @Merlyn: Indeed my solution seems to work only until you add a space somewhere *after* the inner (replicated) double quote. I.e., this still works (displays the echo result): `echo this is "a test"|findstr /c:"this is ""a"`. And this doesn't (complains about not being able to open 'a'): `echo this is "a test"|findstr /c:"this is ""a "`. +1 to your solution, I think you should accept it. – Andriy M Feb 14 '11 at 07:32
  • @Andriy: Thanks for the help, though. I'd like not to have to add all that escaping-noise... – Merlyn Morgan-Graham Feb 14 '11 at 07:34
  • Ugh, the windows command line is terrible. Specifying `""""` (four double-quotes in a row) passes one double-quote as an argument to the called program, which is what I needed. – We Are All Monica May 19 '11 at 19:33