LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2020, 01:45 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
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 ?

Last edited by pedropt; 03-05-2020 at 01:47 PM.
 
Old 03-05-2020, 01:51 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
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.
 
2 members found this post helpful.
Old 03-05-2020, 01:58 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 03-05-2020, 02:03 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
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.
 
3 members found this post helpful.
Old 03-05-2020, 02:04 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 03-05-2020, 02:08 PM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-05-2020, 02:12 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by pedropt View Post
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 View Post
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 View Post
Code:
var="\"my text\""
grep "$var1" myfile
Which case applies to you?
 
Old 03-05-2020, 02:12 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
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

Last edited by Turbocapitalist; 03-05-2020 at 02:19 PM. Reason: missed the 'g'
 
1 members found this post helpful.
Old 03-05-2020, 02:17 PM   #9
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .

Last edited by pedropt; 03-05-2020 at 02:24 PM.
 
Old 03-05-2020, 03:05 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pedropt View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 09:02 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 PM
Double Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 10:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration