I have a linux config file with with format like this:
VARIABLE=5753
VARIABLE2=""
....
How would I get f.e. value of VARIABLE2 using standard linux tools or regular expressions? (I need to parse directory path from file). Thanks in advance.
I have a linux config file with with format like this:
VARIABLE=5753
VARIABLE2=""
....
How would I get f.e. value of VARIABLE2 using standard linux tools or regular expressions? (I need to parse directory path from file). Thanks in advance.
$> cat ./text
VARIABLE=5753
VARIABLE2=""
With perl
regular expression grep
could match these value using lookbehind operator.
$> grep --only-matching --perl-regex "(?<=VARIABLE2\=).*" ./text
""
And for VARIABLE
:
$> grep --only-matching --perl-regex "(?<=VARIABLE\=).*" ./text
5753
eval $(grep "^VARIABLE=" configfile)
will select the line and evaluate it in the current bash context, setting the variable value. After doing this, you will have a variable named VARIABLE
with value 5753
. If no such line exists in the configfile, nothing happens.
You could use the source
(a.k.a. .
) command to load all of the variables in the file into the current shell:
$ source myfile.config
Now you have access to the values of the variables defined inside the file:
$ echo $VARIABLE
5753