2

I've got a file that I need to get a piece of text from using regex. We'll call the file x.txt. What I would like to do is open x.txt, extract the regex match from the file and set that into a parameter. Can anyone give me some pointers on this?

EDIT

So in x.txt I have the following line

$variable = '1.2.3';

I need to extract the 1.2.3 from the file into my bash script to then use for a zip file

5
  • 2
    var=$(grep regex file) or you're going to have to be a lot more specific. Commented Nov 12, 2012 at 2:15
  • Thanks Kevin. First glance, that looks like it may be what I need. I'm actually wanting to extract a group number from the grep (group 1). Can that handle this? Commented Nov 12, 2012 at 2:18
  • You mean like group [0-9]\+? Sure. Or if you just want the number, grep -oP '(?<=group )[0-9]+' Commented Nov 12, 2012 at 2:21
  • No, he probably means var=$(sed -ne 's/^[^=]*= *'\([^']*\)'.*/\1/p' file) Commented Nov 12, 2012 at 2:25
  • Thanks for the quick replies. I've updated my question above to clarify what is in the file and what I need to get Commented Nov 12, 2012 at 2:25

3 Answers 3

7

Use sed to do it efficiently in a single pass:

var=$(sed -ne "s/\\\$variable *= *['\"]\([^'\"]*\)['\"] *;.*/\1/p" file)

The above works whether your value is enclosed in single or double quotes.

Also see Can GNU Grep output a selected group?.

$ cat dummy.txt
$bla = '1234';
$variable = '1.2.3';
blabla
$variable="hello!"; #comment

$ sed -ne "s/\\\$variable *= *['\"]\([^'\"]*\)['\"] *;.*/\1/p" dummy.txt
1.2.3
hello!

$ var=$(sed -ne "s/^\\\$variable *= *'\([^']*\)' *;.*/\1/p" dummy.txt)

$ echo $var
1.2.3 hello!

† or at least as efficiently as sed can churn through data when compared to grep on your platform of choice. :)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I'll have to look into sed as it seems to be a great tool. Unfortunately the above didn't seem to work for whatever reason for me
I see; your input is actually a PHP script, so the line is probably indented; I removed the ^ from the regular expression, go ahead and try again.
And there, since it's a PHP script you're dealing with, I updated the regexp to also deal with double-quotes. :)
Thanks for updating. The code does work however I've already used the code above now which is working. I've upvoted your answer. Thank you again
No problem. I assume the assignment you were interested in is always essentially at the top of the file and cannot preceded by any tests of the form $variable ==... (the grep in the accepted answer would match those instead.) :) Also make sure that you modified the acepted answer to check for double-quoted strings as well as different spacing around = if you think your php source had either, or else you'd be missing values. :)
|
4

You can use the grep-chop-chop technique

var="$(grep -F -m 1 '$variable =' file)"; var="${var#*\'}"; var="${var%\'*}"

1 Comment

Explanation please.
1

If all the file lines have that format ($<something> = '<value>'), the you can use cut like this:

value=$(cut -d"'" -f2 file)

1 Comment

Unfortunately it doesn't as it's a php script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.