LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Grep exact match in double quotes (https://www.linuxquestions.org/questions/programming-9/grep-exact-match-in-double-quotes-4175670800/)

pedropt 03-05-2020 01:45 PM

Grep exact match in double quotes
 
Hi guys , i am struggling with this code for hours and i can not get a perfect solution for it .

Grep with -w switch catches the exact match but i need grep to search a pattern in double quotes .

example :

text

Quote:

"my text is good"
"my"
"my text"
"my text is"
now , consider these grep searches will be under a variable

like :
var1='"my text"'

1st - i can not get pass parsing the double quotes to grep -w
like grep '$var1' myfile

2nd - escaping the grep with
Code:

grep -w '\$var1\" myfile
do not work .

Because when i search without those quotes
example : my text

then grep will pop up lines 1 ,3 and 4 from the quoted text before .

Is there a way to do this ?
tell grep -w to include the double quotes inside the search ?

Turbocapitalist 03-05-2020 01:51 PM

You'll need to escape any double quotes you include inside the regular double quotes:

Code:

var="my text"
grep "\"$var1\"" myfile

or

Code:

var="\"my text\""
grep "$var1" myfile

The escape is so that they are not processed by the shell and get passed to the program grep instead.

pedropt 03-05-2020 01:58 PM

Next time read the part i wrote :
"consider these grep searches will be under a variable"

i wrote that because in fact it is a variable and the text in var does not come escaped .

Turbocapitalist 03-05-2020 02:03 PM

Right. And I gave you two methods to solve that, though spelled the variables wrong. It solves the task you described above.

If the above answer did not solve your task then you'll have to reword it and include the missing material.

pedropt 03-05-2020 02:04 PM

Another example more quickly understandable

textfile : myfile.txt
Quote:

"my text is good"
"my"
"my text"
"my text is"
Code :
Code:

#!/bin/bash
var1=$(sed -n 3p myfile.txt)
catch=$(grep -w '$var1' myfile.txt)
echo "$catch"

I get no output in catch variable because grep is not assuming the quotes inside it .

pedropt 03-05-2020 02:08 PM

yup , sorry i post after you without notice it .
yes , if the var already comes with quotes then i dont need to write the -w switch .

I will test it here on my code .
Thanks

Turbocapitalist 03-05-2020 02:12 PM

No problem. However, based on the update, I would say this way and have the sed script escape the quotes:

Code:

var1=$(sed -n '3p; s|"|\\"|g' myfile)
echo $var1
grep -w "$var1" myfile


astrogeek 03-05-2020 02:12 PM

Quote:

Originally Posted by pedropt (Post 6097511)
Next time read the part i wrote :
"consider these grep searches will be under a variable"

i wrote that because in fact it is a variable and the text in var does not come escaped .

In fairness to Turbocapitalist, I have read it several times and am not sure I understand your intent.

There are two possible cases I see:

1. The double quotes you want to find are not included in the variable value but you want to grep for that value only when surrounded by double quotes, which is handled by Turbocapitalist's first suggestion:

Quote:

Originally Posted by Turbocapitalist (Post 6097507)
You'll need to escape any double quotes you include inside the regular double quotes:

Code:

var="my text"
grep "\"$var1\"" myfile

The escape is so that they are not processed by the shell and get passed to the program grep instead.

2. The double quotes are included in the variable value, which would result from Turbocapitalist's second suggestion and would be found:

Quote:

Originally Posted by Turbocapitalist (Post 6097507)
Code:

var="\"my text\""
grep "$var1" myfile


Which case applies to you?

pedropt 03-05-2020 02:17 PM

Thank you Turbocapitalist , it works like a charm

The code that i tested and worked great was :
Quote:

var=""my text""
grep "$var1" myfile
the others i did not try .

However , adapting my little script with your code would be like this :
Code:

#!/bin/bash
var1=$(sed -n 3p myfile.txt)
catch=$(grep "$var1" myfile.txt)
echo "$catch"

where the -w switch is not needed .

scasey 03-05-2020 03:05 PM

Quote:

Originally Posted by pedropt (Post 6097520)
Code:

#!/bin/bash
var1=$(sed -n 3p myfile.txt)
catch=$(grep "$var1" myfile.txt)
echo "$catch"

where the -w switch is not needed .

You do understand that just running the grep command will print the output on STDOUT, I hope. You don't need to put the result into a variable and then echo the variable:
Code:

#!/bin/bash
var1=$(sed -n 3p myfile.txt)
grep "$var1" myfile.txt

should yield the same result.

I'm not saying that what you're doing is wrong. Just pointing out that it may be unnecessary, unless you have a need for the value in a variable other than to display it.


All times are GMT -5. The time now is 03:03 AM.