torchilidae
torchilidae

Reputation: 3

Checking for a key value pair in the config file using shell script

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

Upvotes: 0

Views: 1253

Answers (2)

torchilidae
torchilidae

Reputation: 3

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

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

Upvotes: 0

Romeo Ninov
Romeo Ninov

Reputation: 7245

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

Upvotes: 1

Related Questions