LinuxQuestions.org
Review your favorite Linux distribution.
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 12-12-2009, 07:40 AM   #1
PMichaud
LQ Newbie
 
Registered: Dec 2009
Posts: 2

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

Last edited by PMichaud; 12-12-2009 at 07:42 AM.
 
Old 12-12-2009, 07:57 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
just the shell
Code:
while read -r line
do
    case "$line" in
        *\$var*)
            line=${line%%;*}
            val=${line##*=}
            ;;
        *define*)
            line=${line#*\'}
            key=${line%%\'*}
            ;;
    esac
    [ ! -z "$val" -a ! -z "$key" ] && echo "$val: $key" && exit
done <"file"
 
Old 12-12-2009, 07:58 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
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.
 
Old 12-12-2009, 09:05 AM   #4
PMichaud
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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?
 
Old 12-16-2009, 01:29 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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".
 
  


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
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
extract substring using sed and regular expressions (regexp) lindylex Programming 20 12-22-2009 10:41 AM
Extract a substring prpersonal Linux - General 8 10-19-2009 02:44 PM
Extract a substring prpersonal Linux - Newbie 2 10-19-2009 08:05 AM
Extract substring matching a regular expression tikit Linux - General 2 02-18-2008 01:47 PM

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

All times are GMT -5. The time now is 11:13 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
Open Source Consulting | Domain Registration