1

I'm trying to write a build script for SLES/RHEL that modifies config files to conform to our company standards. Some configs are formatted as setting = value and some are just setting value. I'm using sed currently and feel I'm almost there but it's not working when there is no equals sign.

Current code: (Sorry I'm not very good at formatting these properly yet...)

$ cat test.conf
setting1 value1 
setting2= value2
setting3 = value3
setting4 =value4 

Current (test) function:

#!/bin/bash
replace () {  
file=$1  
var=$2  
new_value=$3



sed -e "s/^$var=.*/$var = $new_value/" -e "s/^$var =.*/$var = $new_value/" -e "s/^$var.*/$var = $new_value/" -e "s/^$var /$var $new_value/" "$file"|grep $var  

}


replace test.conf setting1 value1new 
replace test.conf setting2 value2new  
replace test.conf setting3 value3new  
replace test.conf setting4 value4new

However I get this as a result:

$ ./functiontest
setting1 value1new= value1new 
setting2 value2new= value2new 
setting3 value3new= value3new 
setting4 value4new= value4new

It works if I take out the last sed portion, but only where there is an equals sign.

Any ideas what I'm doing wrong?

5
  • The goal that I am going for is for the final product to look like this: setting1 value1new setting2 = value2new setting3 = value3new setting4 = value4new Commented May 11, 2017 at 19:57
  • 1
    Really the whole idea of regular expressions is to avoid matching every variant pattern case-by-case: for example, you could match zero or more spaces followed by an optional equals sign "s/^${var} *=\{0,1\}.*/${var} = ${new_value}/" Commented May 11, 2017 at 20:10
  • What was the last portion meant to do anyway? Commented May 11, 2017 at 20:43
  • @Philippos is that portion question directed at me or @steeldriver? Commented May 11, 2017 at 20:47
  • @steeldriver I fully agree. I'm no expert at scripting though I never intended to create a case-by-case match, I just didn't know how to do a "catch all" statement. Thanks! :) Commented May 11, 2017 at 20:48

2 Answers 2

0

You can simplify the set of "similar" sed substitutions to the following:

cat replace.sh 
#!/bin/bash
replace () {
var=$2  
new_val=$3

sed -e "s/^$var *= *.*/$var = $new_val/; s/^$var [^=]*$/$var $new_val/" "$1" | grep $var 

}

replace test.conf setting1 value1new 
replace test.conf setting2 value2new  
replace test.conf setting3 value3new  
replace test.conf setting4 value4new

Usage:

bash replace.sh

The output:

setting1 value1new
setting2 = value2new
setting3 = value3new
setting4 = value4new
0
0
sed -n "s/^$var[ =].*/$var = $new_value/p" "$file"

It's sufficient to follow the variable by either space or equal sign, then one replacement fits all.

By using option -n and the p after the replacement you don't need the grep. The sed did already grep the right line.

1
  • Sorry I should have pulled the grep, it was just for my troubleshooting, as was the lack of inclusion of sed -i. Commented May 11, 2017 at 20:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.