Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Use a character class

findstr /srn ['\^"] *.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top