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

Share:
33,881
pat
Author by

pat

Updated on July 09, 2022

Comments

  • pat
    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
    fedorqui almost 9 years
    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
    fedorqui almost 9 years
    To make it works always, use ^FOO=. This way, it will match the beginning of the line.
  • CrazyMenConnected
    CrazyMenConnected almost 9 years
    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!
  • fedorqui
    fedorqui almost 9 years
    these are quite common tools with common approaches. If anybody needs some explanation, just ping me and I will add them!
  • Bruno Bieri
    Bruno Bieri over 7 years
    sed does the job quite well.
  • Nandhakumar Kittusamy
    Nandhakumar Kittusamy about 5 years
    how to grep a value from FOO in entire path instead of giving a particular file?
  • fedorqui
    fedorqui about 5 years
  • Thiago Figueiro
    Thiago Figueiro almost 5 years
    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
    Thiago Figueiro almost 5 years
    Careful 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 both oops and bar. Use FS='^FOO=' to get the exact match.
  • MaXi32
    MaXi32 over 3 years
    I love the sed answer
  • MaXi32
    MaXi32 over 3 years
    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
    MaXi32 over 3 years
    This is the perfect answer if you make little modification like what @ThiagoFigueiro told. (it has been 4 years).
  • MaXi32
    MaXi32 over 3 years
    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
  • Thiago Figueiro
    Thiago Figueiro over 3 years
    I tried to update the answer 4 years ago but SO doesn't allow 1-character changes so 🤷🏻‍♂️
  • payne
    payne over 2 years
    Using f-2-, as suggested in a comment above, seems to be wrong. Using -f2- works for me.