Extract a substring from a line in a faill
I'm new to linux and bash, and all the awesome utilities linux has, so I'm struggling with the syntax here.
Basically, I have a configuration file that's roughly: <?php $var = 'dsfsdf'; //sdfsdf define('KEY', 'ftyurghj'); //etc. ?> My goal is to extract the values of $var and 'KEY' The issues I'm having are A) I'm having a heck of a time escaping some characters like "=" I've done something like this: sed -ne '/^\$var/p' file.php And gotten the right line back, but when I go further and try sed -ne '/^\$var \= /p' file.php It starts complaining. I would ideally want it to match any whitespace so it could read: $var ________ = __________ 'blah'; without choking. and B) I don't know how to capture matches using grep or sed or whatever I need to use sed -ne '/^\$var \= \'([.*])\'\;/p' file.php So let's pretend that regex worked (even though it actually just chokes). I want the contents of the parens. How should I approach this? |
just the shell
Code:
while read -r line |
Code:
sed -n "/<\?php/,/^?>/{ /$var *= */s/\$var *= *'\([^']*\)'.*/\1/p |
Thanks for the help!
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? |
The "/<\?php/,/^?>/" at the beginning defines a range of lines. The brackets "{}" then enclose the sed commands to run when inside that range.
Yes, I should have escaped the "$" but it worked when I tested it and was to lazy to go back and escape it. I put the closing bracket on another line to emphasize that you could add another line for the line with "define". |
All times are GMT -5. The time now is 06:05 AM. |