LinuxQuestions.org
Help answer threads with 0 replies.
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 09-10-2011, 04:54 PM   #1
pauld
Member
 
Registered: Nov 2010
Posts: 33

Rep: Reputation: 1
using variable inside regex


So I have the following command/regex with which I want to use a variable:

Code:
printf '%s\n' 'g/^that.*dog/s/^/;/\' '-s' w | ed -s file1
Instead of "dog", I want to have a variable called $PET
However, any way I put it, its not working.

Can someone please advise what I'm doing wrong. Thanks!
 
Old 09-10-2011, 05:00 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You need to use the variable outside of single quotes.
Code:
'g/^that.*'"$PET"'/s/^/;/\'
Kevin Barry
 
1 members found this post helpful.
Old 09-10-2011, 05:08 PM   #3
pauld
Member
 
Registered: Nov 2010
Posts: 33

Original Poster
Rep: Reputation: 1
Hi Kevin,
Thank you for your response. In your example, you have the variable inside a single and then double quotes.
I tried this and that didn't work
 
Old 09-10-2011, 05:13 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by pauld View Post
Hi Kevin,
Thank you for your response. In your example, you have the variable inside a single and then double quotes.
I tried this and that didn't work
The red ' are to close then reopen the single quoting, so the " are entirely outside of quotes. What does echo 'g/^that.*'"$PET"'/s/^/;/\' show? If it doesn't work that's probably an indication that $PET is blank.
Kevin Barry
 
1 members found this post helpful.
Old 09-10-2011, 05:31 PM   #5
pauld
Member
 
Registered: Nov 2010
Posts: 33

Original Poster
Rep: Reputation: 1
Ahh thanks... it seems to work now... I duno why I had to export it first.. Thank you for your help!
 
Old 09-10-2011, 05:55 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can also double-quote the entire string instead, and then escape the few characters that are still considered special inside them. These are $`\ (plus ! in interactive shells).
Code:
printf '%s\n' "g/^that.*$PET/s/^/;/\\" '-s' w | ed -s file1
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
Old 09-11-2011, 12:51 AM   #7
pauld
Member
 
Registered: Nov 2010
Posts: 33

Original Poster
Rep: Reputation: 1
Thanks David. Would the $ be considered special character to be escaped with "\" if it identifies a variable?
I tried doing that but it didn't work. I must have done something wrong. But Kevin's way worked fine. Thank you both for your feedback!
 
Old 09-11-2011, 04:55 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quoting/escaping protects otherwise reserved characters from interpretation by the shell, so you escape them when you need their literal value, and you don't escape them when you need the shell to process them as part of the shell syntax.

In your example, the double quotes escape all but two characters in the string; the $ and \ are still considered special. Since you need "$PET" to be expanded by the shell, you don't escape the dollar sign. But the backslash at the end of the string needs to be literal, so you do escape that, with another backslash. Otherwise the single backslash would be interpreted as escaping the second " quotemark, which breaks the shell syntax.

As an exercise in understanding, try reading each character in the string from left-to-right, and determine whether the shell would interpret it, or if it's protected in some way.

Oh, and don't forget that single quotes are escaped inside double quotes, and vice versa.
 
1 members found this post helpful.
Old 09-11-2011, 10:20 AM   #9
pauld
Member
 
Registered: Nov 2010
Posts: 33

Original Poster
Rep: Reputation: 1
Thank you David. That helped me better understand!
 
Old 09-11-2011, 11:52 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Oh, and don't forget that single quotes are escaped inside double quotes, and vice versa.
One last quoting idiosyncrasy is worth mentioning: Although you can escape " within " ", e.g. echo "the \" character is a double quote", you can't escape ' within ' '; therefore, e.g. echo 'the \' character is a single quote' isn't valid syntax.
Kevin Barry
 
Old 09-15-2011, 09:33 AM   #11
pauld
Member
 
Registered: Nov 2010
Posts: 33

Original Poster
Rep: Reputation: 1
Hi Kevin,
Yes you're right. I tested that. The single quotes (') take everything inside it literally. All variables, everything.
Interesting way bash does things.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 08:38 AM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 11:31 AM
Using Regex to make a search inside Less ?? maginotjr Programming 3 01-23-2009 05:04 AM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

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

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration