How to grep for value in a key-value store from plain text
33,881
Solution 1
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
Solution 2
Simple way:
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-
.
Solution 3
Try this one too...
grep "^FOO=" file.txt | awk -F"=" '{ print $2 }'
With some help from @fedorqui
Author by
pat
Updated on July 09, 2022Comments
-
pat almost 2 years
Given a plain text file containing
FOO=foo BAR=bar BAZ=baz
How do we
grep
for the value using the key? -
fedorqui almost 9 years
grep
+awk
normally means thatawk
alone can handle it. Also, what if we have a linehello=FOO
? This would returnFOO
, whereashello
is not the given key. -
fedorqui almost 9 yearsTo make it works always, use
^FOO=
. This way, it will match the beginning of the line. -
CrazyMenConnected almost 9 yearsAh thanks, I didn’t knew that! I was trying to help someone with a doubt, and I was the one that ended up learning something! Thanks @fedorqui!
-
fedorqui almost 9 yearsthese are quite common tools with common approaches. If anybody needs some explanation, just ping me and I will add them!
-
Bruno Bieri over 7 years
sed
does the job quite well. -
Nandhakumar Kittusamy about 5 yearshow to grep a value from FOO in entire path instead of giving a particular file?
-
fedorqui about 5 years@NandhaKumar you may want to check How do I find all files containing specific text on Linux?
-
Thiago Figueiro almost 5 yearsBeware that this won't return the correct value if the value has
=
in it. E.g.:echo 'FOO=bar=oops' | grep "^FOO=" | cut -d"=" -f2
incorrectly returnsbar
. Usingf-2-
returns the correct valuebar=oops
-
Thiago Figueiro almost 5 yearsCareful with
awk -v FS="FOO="
because it will return the value for any keys with partial matches; e.g.:echo -en 'AFOO=oops\nFOO=bar' | awk -v FS='FOO=' 'NF>1{print $2}'
returns bothoops
andbar
. UseFS='^FOO='
to get the exact match. -
MaXi32 over 3 yearsI love the sed answer
-
MaXi32 over 3 yearsThis won't work when you have value like this (as seen in complex password):
echo 'FOO==FO=bar=oo==ps=pk' | grep "^FOO=" | awk -F"=" '{ print $2 }'
, it returns nothing. it should return=FO=bar=oo==ps=pk
. I think the best answer when using grep is from @Inanc Gumus with little modification from @Thiago's comment -
MaXi32 over 3 yearsThis is the perfect answer if you make little modification like what @ThiagoFigueiro told. (it has been 4 years).
-
MaXi32 over 3 yearsTry this and you will be disappointed with this answer:
echo 'FOO==FO=bar=oo==ps=pk' | grep "FOO=" | sed -e 's/.*=//'
This should return=FO=bar=oo==ps=pk
but it returns onlypk
-
Thiago Figueiro over 3 yearsI tried to update the answer 4 years ago but SO doesn't allow 1-character changes so 🤷🏻♂️
-
payne over 2 yearsUsing
f-2-
, as suggested in a comment above, seems to be wrong. Using-f2-
works for me.