Escape Double Quote in Grep

Escape double quote in grep

The problem is that you aren't correctly escaping the input string, try:

echo "\"member\":\"time\"" | grep -e "member\""

Alternatively, you can use unescaped double quotes within single quotes:

echo '"member":"time"' | grep -e 'member"'

It's a matter of preference which you find clearer, although the second approach prevents you from nesting your command within another set of single quotes (e.g. ssh 'cmd').

How to match double quotes using grep

Use single quotes to consider literal meaning of every character.

grep 'and "Ben" is' trial.txt

How do I escape a double quote with GNU grep for windows/dos?

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.

how to escape single quotes in grep

Just wrap the string within double quotes:

grep "'true' format" your_file

Test

$ cat a
this is 'true' format
and this is true formatting
test

$ grep "'true' format" a
this is 'true' format

How to search for a string with one double quote with git grep in Windows cmd

In Git CMD or Windows cmd console, you can use ^ to escape the " inside the terminal:

git grep ""^""Agent" > 1.txt

In Git Bash, you can simply use

git grep '"Agent' > 1.txt

See this text I made on my data with "Regex pattern:

Sample Image

grep doesn't search double quotes well. not even with \

The issue with your command is not the quotes but the backslashes. As you have the backslashes in your original file, you need to match the backslashes with grep too (so, escape them):

$ cat myfile.txt | grep 's:15:\\"https://cnn.com\\"'
s:15:\"https://cnn.com\"

grep with double quotation in bash script

The simplest adaptation of the code you're using is:

for ip in $(cat /tmp/iplist)
do
grep "$ip" /tmp/whitelist
done

Once you've got the value in the variable, its double quotes aren't damaged by further double-quoted variable expansion. If the /tmp/iplist file doesn't contain the double quotes but they're critical, then you can use:

grep "\"$ip\"" /tmp/whitelist

or you could use this:

grep \""$ip"\" /tmp/whitelist

(and there are two asymmetric permutations available too). It's a good idea to make sure you do know why that works. There are some ways to use single quotes if you want to, but the "$ip" part must be outside of the single quotes.

All your examples start the pattern argument to grep with a single quote. Single quotes suppress all expansions until the next single quote. So, for example, grep '"$ip"' /tmp/whitelist is looking for 5 characters — ", $, i, p, " — in the file. In none them is the variable ip ever expanded.

There will be problems if any IP address ever gets a space in it. Be cautious about using for ip in $(cat /tmp/iplist). Very often you'd do better with:

while read -r ip
do
grep "$ip" /tmp/whitelist
done < /tmp/iplist

Another way to do this is with grep -F or fgrep:

grep -F -f /tmp/iplist /tmp/whitelist

This doesn't insist on double quotes around the IP addresses, but makes a single pass over the /tmp/whitelist file (and a single pass over the /tmp/iplist file too), which is about as efficient as it gets. This will produce lines in a slightly different order from before, which probably won't matter, but you should be aware of it.

If you must have the double quotes (to avoid selecting 11.2.3.44 when searching for 11.2.3.4, then:

grep -F -f <(sed 's/^/"/; s/$/"/' /tmp/iplist) /tmp/whitelist

This uses process substitution to pass an edited version of the /tmp/iplist file to the grep command. If you don't have process substitution in your shell, you can probably use:

sed 's/^/"/; s/$/"/' /tmp/iplist | grep -F -f - /tmp/whitelist

which makes grep read the list of patterns to match from standard input instead of a named file. If perchance -f - doesn't work (e.g. because you're working on a Mac using macOS, or probably a BSD machine), then -f /dev/stdin probably will, or -f /dev/fd/0.

You could also generate the /tmp/iplist file with the double quotes in place. You could generate the /tmp/whitelist file without the double quotes and then use grep -x to specify an exact match.

In case you haven't already gathered, there are quite a lot of different ways to do this.

How to find a double quoted string using grep and regex?

Your expression doesn't make sense to me. It seems like you're looking for:

grep -R '5\.1' .

Or if you want to include the " characters:

grep -R '"5\.1"' .

But I don't see anything related to those in your original post, so it's hard to say.

Grep string inside double quotes

Try doing this :

$ cat aaaa
foo"bar"base
$ grep -oP '"\K[^"\047]+(?=["\047])' aaaa
bar

I use look around advanced regex techniques.

If you want the quotes too :

$ grep -Eo '["\047].*["\047]'
"bar"

Note :

\047

is the octal ascii representation of the single quote

bash: matching single and double quotes together by grep

I am probably mis-interpreting the question, but it seems like you are asking how to deal with properly escaping the quotes. Just do:

grep '\(["'"']\).*\1.*\1"


Related Topics



Leave a reply



Submit