I'm trying to search for
Assembly="Foobar
in all files and direct the output to tmp.txt
I've tried (cmd/error)
grep -r "Assembly=\"Foobar" . >> tmp.txt error (invalid argument)
grep -r "Assembly=""Foobar" . >> tmp.txt
error (not enough space)grep -r 'Assembly="Foobar' . >> tmp.txt
error (not enough space)
very frustrating. Any suggestions?
inside double-quoted strings, use `" or "" to escape double-quotes. inside single-quoted strings, use '' to escape single-quotes.
Generally, the backslash (\) character is yet another option to quash the inbuilt shell interpretation and tell the shell to accept the symbol literally.
Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings. Alternative forms for the last two are '\u{nnnn}' and '\U{nnnnnnnn}'. All except the Unicode escape sequences are also supported when reading character strings by scan and read.
You need to worry about escaping quotes for both grep and cmd.exe
grep expects quotes to be escaped as \"
cmd.exe escapes quotes as ^"
. The quote is a state machine, meaning unescaped quotes toggle quote semantics on and off. When quoting is on, special characters like <
, |
, &
, etc are treated as literals. The first unescaped quote turns on quote semantics. The next quote turns quote semantics off. It is only possible to escape a quote when quoting is OFF. Unbalanced quotes result in the remainder being quoted. So in your case, the >>tmp.txt
is not treated as redirection by cmd.exe, so it gets passed to grep as an argument.
Any of the following will work:
grep -r "Assembly=\"Foobar^" . >>tmp.txt
grep -r ^"Assembly=\"Foobar" . >>tmp.txt
grep -r ^"Assembly=\^"Foobar^" . >>tmp.txt
grep -r Assembly=\^"Foobar . >>tmp.txt
If your search term included spaces, then grep needs the enclosing quotes, so you would need any of the first three forms. The last form would not work with spaces in the search term.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With