2

I'm trying to get lines from files in a directory tree with a single or double quote in them. As an example, I want to get these lines with a single findstr command:

  • You should be able to get a "Hello world" program really quick.
  • Enter a value for params 'name' and 'age', please.
  • If no value is entered for 'name' param, the "Hello world" program will throw an exception.

I can get lines with only single quotes (findstr /srn \' *.txt), only double quotes (findstr /srn \^" *.txt), or both single and double quotes (findstr /srn \'\^" *.txt), but I need lines with single or double quotes with only a single command.

Any idea about how to achieve it?

jgg
  • 967
  • 2
  • 10
  • 19

2 Answers2

4

Explosion Pills had the correct idea, but not the correct syntax.

It can get tricky when trying to escape the quote for both FINDSTR and for the CMD parser. The regex you want is ['\"], but then you need to escape the " for the CMD parser.

This will work:

findstr /srn ['\^"] *.txt

and so will this

findstr /srn "['\"]^" *.txt

and so will this

findstr /srn ^"['\^"]^" *.txt


NOTE
You are at risk of failing to find files that contain the string because of a nasty FINDSTR bug. The /S option may fail to find files if 8.3 short names are enabled and a folder contains a file with an extension longer than 3 chars that starts with .txt. (name.txt2 for example). For more information, see the section labeled BUG - Short 8.3 filenames can break the /D and /S options at What are the undocumented features and limitations of the Windows FINDSTR command?.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

Use a character class

findstr /srn ['\^"] *.txt
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • If I try this the command line blocks until I stop it, without printing anything by the way. I read that the double quote might cause a problem in this case, and I would like to know a work around for that issue. – jgg May 28 '13 at 07:37
  • @jgg - It is not possible to escape `"` as `^"` once quoting is already started with an earlier `"`. See [my answer](http://stackoverflow.com/a/16795153/1012053) for the correct method to escape the `"`. – dbenham May 28 '13 at 14:58