16

Given a plain text file containing

FOO=foo
BAR=bar
BAZ=baz

How do we grep for the value using the key?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
pat
  • 1,947
  • 2
  • 14
  • 18
  • A dirty alternative: If the file looks like what your have pasted, source the file and use $BAR, $BAZ directly. PS: Dont follow this suggestion, you'll instantly encounter many corner cases. – UltraInstinct Jun 11 '15 at 09:21

4 Answers4

37

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
Flow
  • 23,572
  • 15
  • 99
  • 156
fedorqui
  • 275,237
  • 103
  • 548
  • 598
11

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-.

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
  • 2
    Beware 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 returns `bar`. Using `f-2-` returns the correct value `bar=oops` – Thiago Figueiro Jun 10 '19 at 01:54
  • This is the perfect answer if you make little modification like what @ThiagoFigueiro told. (it has been 4 years). – MaXi32 Jan 09 '21 at 23:26
  • I tried to update the answer 4 years ago but SO doesn't allow 1-character changes so ‍♂️ – Thiago Figueiro Jan 22 '21 at 07:02
  • Using `f-2-`, as suggested in a comment above, seems to be wrong. Using `-f2-` works for me. – payne Dec 02 '21 at 01:36
2

Try this one too...

grep "^FOO=" file.txt | awk -F"=" '{ print $2 }'

With some help from @fedorqui

  • `grep` + `awk` normally means that `awk` alone can handle it. Also, what if we have a line `hello=FOO`? This would return `FOO`, whereas `hello` is not the given key. – fedorqui Jun 11 '15 at 09:24
  • To make it works always, use `^FOO=`. This way, it will match the beginning of the line. – fedorqui Jun 11 '15 at 09:37
  • 1
    Ah 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! – CrazyMenConnected Jun 11 '15 at 09:40
  • This 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 Jan 09 '21 at 23:20
0

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

Bruce
  • 7,094
  • 1
  • 25
  • 42
  • Try 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 only `pk` – MaXi32 Jan 09 '21 at 23:28