Is there a way to escape quotes in ripgrep for MS Windows (Powershell or CMD)?
Asked Answered
A

3

9

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?

Anthony answered 2/1, 2020 at 20:51 Comment(2)
What's stopping you from using built-in Windows methodology and/or tools instead of ripgrep?Treharne
@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).Outclass
O
6

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.

Outclass answered 2/1, 2020 at 23:17 Comment(1)
These also seems to work. Powershell: rg '\"Hello' , CMD: rg "\"Hello"Anthony
D
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.
Degradable answered 8/3, 2022 at 8:35 Comment(1)
Thanks. This works in cmd. It doesn't work in powershell (and not sure why)Anthony
P
0

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

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

file:1:"hello

Paraguay answered 3/1, 2020 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.