7

I am using Linux and bash. I have a simple text file like below:

VAR1=100
VAR2=5
VAR3=0
VAR4=99

I want to extract by means of bash the value of VAR2, that is 5.

How could I do that?

4
  • Use grep and cut Commented Feb 9, 2016 at 16:17
  • Or just use awk with = as the field split character. Commented Feb 9, 2016 at 16:17
  • 1
    Or source simple_text_file; echo $VAR2. Commented Feb 9, 2016 at 16:18
  • Or sed -n 's/^VAR2=\([0-9]*\)$/\1/p' simple_text_file. Commented Feb 9, 2016 at 16:33

5 Answers 5

10

Assuming the file is called vars.txt

sed -n 's/^VAR2=\(.*\)/\1/p' < vars.txt

You can use the value elsewhere like this using single back quotes

echo VAR2=`sed -n 's/^VAR2=\(.*\)/\1/p' < txt`
Sign up to request clarification or add additional context in comments.

3 Comments

sed -n 's/^VERSION=\(.*\)/\1/p' < vars.txt
I don't understand what is written in these single quotes. What are they called and what is a good tutorial to learn this?
@AnimeshKumar that is the sed s command which follows this syntax pattern and includes use of regex: s/regexp/replacement/
6

The simplest way might be to use source or simply . to read and execute the file. This would work with your example, because there are no spaces in the variable values. Otherwise you need to use grep + cut or awk, as stated in other answers.

. /path/to/your/file
echo $VAR2

[edit] As stated by dawg, this would make the other variables available in your script too, and possibly overwrite existing variables.

1 Comment

He may not want the value of the other items set. source file will set all the variables to values in the file...
2

Given:

$ echo "$txt"
VAR1=100
VAR2=5
VAR3=0
VAR4=99

You can use awk:

$ echo "$txt" | awk -F= '/^VAR2/ { print $2 }'
5

Or grep and cut:

$ echo "$txt"  | egrep '^VAR2=\d+' | cut -d = -f 2
5

On Bash, you can insert the value of those assignments into the current shell using source and filter the lines you wish to use. In this case, only the line VAR2=5 will be used. You need to write that to a file and then source that file:

$ echo "$txt"  | grep '^VAR2' > tmp && source tmp && rm tmp
$ echo "$VAR2"
5

Or use process substitution to eliminate the tmp file. You can use either grep or awk here:

$ source <(awk '/^VAR2/{print}' <<< "$txt")
$ echo "$VAR2"
5

Comments

1

Assume this as your txt file, named test.txt

VAR2 = 5
VAR3 = 0
VAR4 = 99

you can cat test.txt | grep 'VAR2' | awk '{printf $3}'

and then your output will be: 5

Here, cat test.txt will display the content of test.txt in your terminal,grep 'VAR2' will list lines containing 'VAR2' and awk '{printf $3}' will print the value of the variable

3 Comments

That doesn't work. Did you test this? Where? How?
I guess i have mistaken. Let me correct that, and thank you for pointing it out
Sorry, the remaining downvote is not mine, I cannot remove it.
0

For the files as described, you can just source the file as bash script which will run it's content and update you workspace environment with it. For example:

source file.txt
echo $VAR2

Comments

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.