ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed -n "/<\?php/,/^?>/{ /$var *= */s/\$var *= *'\([^']*\)'.*/\1/p
}" testfile
Escaping the single quote is potentially the trickiest part. I did it here by putting the sed line inside double quotes. The dollar sign also needs to be escaped in the LHS so it is taken literally. You can insert a second line inside the brackets to match define line.
I'm trying to figure out what's going on using my crude knowledge of sed.
The bracket is on a new line, is that necessary?
When I try to use what you wrote, with new line, something strange happens. I paste it in, and without pressing enter, it seems to execute, and it lists all the files in the current directory in 2 columns with a ">" at the end. It repeats that list maybe 2 dozen times (all in less than a second). Don't know what that's about
When I remove the tabs and new line, I just don't get a match, so I execute, get nothing back, and am back at the prompt.
From what I can gather though, you're saying:
use sed, without printing the normal output. We're looking between <?php and ?> for a string matching "/$var *= */s/\$var *= *'\([^']*\)'.*/", and we'll grab the first reference we get, and print it.
The pattern is confusing though.
/$var *= */s/\$var *= *'\([^']*\)'.*/
Doesn't the $ need to be escaped? What about the =? I get the zero ro more spaces, and I get the reference matching zero or more characters that aren't ', but why does it try to match ".*" after that?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.