7

I want to find a string "Hello (Hello starts with double quote) in text files using ripgrep.

Normally, in Bash or ZSH, this would work by escaping with backslash or surrounding with single quote:

rg \"Hello
rg '"Hello'

However, in MS Windows (Powershell and CMD), I've tried these but none of these worked:

rg \"Hello
rg '"Hello'
rg `"Hello
rg '`"Hello'

Is there any way to escape single or double quotes using ripgrep in MS Windows?

Daniel Kim
  • 926
  • 1
  • 9
  • 17
  • What's stopping you from using built-in Windows methodology and/or tools instead of ripgrep? – Compo Jan 02 '20 at 21:42
  • 2
    @Compo: One compelling reason to use ripgrep is its speed; another is its friendly output formatting (highlighting of the matching line parts). While `Select-String` in PowerShell 7.0 now has such highlighting too, it is much slower. Using `Select-String` from within PowerShell would avoid the escaping headaches, but you'd face the same ones if you were to call `findstr.exe` (whose regex support is poor). – mklement0 Jan 03 '20 at 09:46

3 Answers3

5

Verbatim string "Hello must ultimately be passed as \"Hello to rg ("\"Hello" would work too). That is, the verbatim " char. must be \-escaped:

From cmd.exe:

rg \^"Hello

^, cmd.exe's escape character, ensures that the " is treated verbatim and is removed by cmd.exe before calling rg.
Note that ^ isn't strictly necessary here, but it prevents the " from being considered the start of a double-quoted argument, which could make a difference if there were additional arguments.

From PowerShell:

rg \`"Hello

`, PowerShell's escape character, ensures that the " is treated verbatim and is removed by PowerShell before calling rg.


Arguably, the explicit \-escaping shouldn't be necessary, because it is the duty of a shell to properly pass arguments to target executables after the user has satisfied the shell's own escaping requirements (escaping the verbatim " with ^ in cmd.exe, and with ` in PowerShell).

In the context of PowerShell, this problematic behavior is summarized in this answer.

Note that in PowerShell this extra escaping is only needed if you call external programs; it isn't needed PowerShell-internally - such as when you call Select-String, as shown in js2010's answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
2

You can use rg -F \"Hello

   -F, --fixed-strings 
   Treat the pattern as a literal string instead of a regular expression. When this flag is used, special regular expression meta characters such as .(){}*+ do not
   need to be escaped.

   This flag can be disabled with --no-fixed-strings.
Fangxing
  • 5,716
  • 2
  • 49
  • 53
0

If you're in powershell you might as well do:

get-childitem file | select-string '"hello'

file:1:"hello

js2010
  • 23,033
  • 6
  • 64
  • 66