0

I have a configuration file in which I want to check if the the key value pair are present as below and I want them to be a exact match.

key=value xxxxx xxxx

After the key value there can be a space or a tab as shown in the above so I cannot use a simplegrep -q for getting the key value pair.

Can someone please help me with this

  • [This](https://stackoverflow.com/q/1494178/1707353) might be helpful. – Jeff Holt Mar 02 '20 at 03:08
  • `fmt -1 ConfigFile | grep -qxF 'key=value'` or `grep -q '^key=value$ ConfigFile` or `grep -qE '^key=value([ \t].*)?$' ConfigFile` depending on what you want to happen if you find space/tab – jhnc Mar 02 '20 at 04:00
  • @jhnc the above command works for me thank you very much. If possible can you please post it as an answer. – torchilidae Mar 02 '20 at 05:09

2 Answers2

1

Script like this can extract the value:

awk '/key/ {split($1,a,"=");print a[2]}' input_file

or with variable

KEY="key"
awk -v key=$KEY '$1 ~ key {split($1,a,"=");print a[2]}' input_file
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
0

The below command worked out for me as suggested by @jhnc

sudo fmt -1 FILEPATH | grep -qxF 'KEY=VALUE'