Given a plain text file containing
FOO=foo
BAR=bar
BAZ=baz
How do we grep for the value using the key?
Given a plain text file containing
FOO=foo
BAR=bar
BAZ=baz
How do we grep for the value using the key?
Use a look behind:
$ grep -Po '(?<=^FOO=)\w*$' file
foo
I also like awk for it:
$ awk -v FS="FOO=" 'NF>1{print $2}' file
foo
Or even better:
$ awk -F= -v key="FOO" '$1==key {print $2}' file
foo
With sed:
$ sed -n 's/^FOO=//p' file
foo
Or even with Bash -ONLY if you are confident about the file not containing any weird values-, you can source the file and echo the required value:
$ (source file; echo "$FOO")
foo
grep "^FOO=" | cut -d"=" -f2-
I prefer this because it's very easy to remember for me.
Explanation: It simply greps the line starting with FOO (hence the ^) and cutting it with = to pieces and then getting the second piece with -f2-.
 
    
    Try this one too...
grep "^FOO=" file.txt | awk -F"=" '{ print $2 }'
With some help from @fedorqui
 
    
    Another proposition : split the work
grep "FOO=" file.txt | sed -e 's/.*=//'
grep will fetch the correct lines
sed will remove anything up to the (last) equal sign
