0

I have shell script and as part of the script I want to update the a.conf file (it is configuration file) which includes key-value pair, I want to update the values of the key. Script ask user to input the values which I am storing into variables. How could I update the multiple values in below file by using those variables. I tried sed but not sure how to use it for only to modify value of a particular key

a.conf file

key1=value1
key2=value2

I tried below sed command but it is not updating the value of given key -

sed -i -e "s/\($key1 *= *\).*/\1$newvalue/" $CONFIG_FILE

Update

If I update above command by removing space between *=*\ then, newvalue get appended to existing value e.g. key1=value1newvalue

Not sure what I am missing here

5
  • Could you please confirm if it's a json file(if yes then kindly do update your question with same format)? Also please do add your tried sed code in your question, thank you. Commented Aug 7, 2022 at 18:43
  • I have update the question. This is configuration file (.conf), this is not a json file. Commented Aug 7, 2022 at 18:47
  • Kindly check this link stackoverflow.com/questions/29749824/… if this helps you to understand how to use shell variable into sed 🙏 Commented Aug 7, 2022 at 18:49
  • Is the T after $key1 supposed to be there? Commented Aug 7, 2022 at 19:14
  • Hi Dan, Sorry it was a typo. Commented Aug 7, 2022 at 19:40

3 Answers 3

0

I found one way to modify values of a particular key in a given file of format of -

key1=value1
key2=value2

sed -i "s/key1.*/key1=$newvalue/" "$CONFIG_FILE"
Sign up to request clarification or add additional context in comments.

Comments

0

Here I read the a.conf line-by-line and define the key and val as variables. The user is asked to input the values, which are replaced in the a.conf using sed.

#!/bin/bash

CONFIG_FILE=a.conf
cat $CONFIG_FILE | while read line; do
   echo "Processing line: $line"
   key=$(echo $line | awk -F= '{print $1}');
   read  -p "Enter value for $key " val < /dev/tty
   sed -ie "s/^$key=.*/$key=$val/" "$CONFIG_FILE"
done

Trial run:

$ cat a.conf
key1=value1
key2=value2

$ ./script.sh
Processing line: key1=value1
Enter value for key1 test1
Processing line: key2=value2
Enter value for key2 test2

$ cat a.conf
key1=test1
key2=test2

Comments

0

Using Bash Script to update the text via sed

This script first runs the cd to the location of the file and then runs the sed command to replace the old value #CONFIG_PACKAGE_oui-httpd=y with CONFIG_PACKAGE_oui-httpd=y in the file named target.config.

Logic: sed -i 's/old_text/New_Text/' filename.sh

#! /bin/bash
cd /target/linux/gem6xxx/xs5g01_cpe/ 
sed -i 's/#CONFIG_PACKAGE_oui-httpd=y/CONFIG_PACKAGE_oui-httpd=y/' target.config

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.