0

I have a file call config.txt which contains something like this.

[MS-SQL]
DRIVER : FreeTDS
SERVER : 138.23.21.45

What I need to do is, Whatever the string values which contains after SERVER : need be replaced with a content in a shell variable like $SERVER_IP.

Final config.ini need to be like this. (consider bash shell variable consist some this like $SERVER_IP=192.168.5.3 )

[MS-SQL]
DRIVER : FreeTDS
SERVER : 192.168.5.3
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • 2
    Possible duplicate of [How do I use sed to change my configuration files, with flexible keys and values?](http://stackoverflow.com/questions/5955548/how-do-i-use-sed-to-change-my-configuration-files-with-flexible-keys-and-values) – nu11p01n73R Dec 01 '16 at 06:28
  • I am new to sed as well as for the shell scripting. It will be better if you explain me the way (syntax). –  Dec 01 '16 at 06:30
  • 1
    Please check the duplicate link I have posted. It has a very good example of how you can do it. If you run into some issues with that, add it to the question. Everyone will be happy to help you once you have tired something. – nu11p01n73R Dec 01 '16 at 06:31

1 Answers1

0

This will change the line:

sed -iE 's/(SERVER : )([0-9.]+)/\1'"$SERVER_IP"'/' config.txt

Description:

-i                    # write changes to the file "in place".
-E                    # Use extended regex (to avoid the need
                      # of backslash in `()`)
's/ … / … /'          # Use the substitute command.
(SERVER : )           # Match the string you need `SERVER : ` capturing it
                      # in the first group of parenthesis.
([0-9.]+)             # capture digits and dots in the second group.
/\1'"$SERVER_IP"'/'   # write the contents of the first group and  
                      # the value of the variable to the file.
config.txt            # name of the file.

You may also change the value after DRIVER, if you want:

sed -iE 's/(DRIVER : )(.*)/\1ODBC/' config.txt
  • Use `sed -i -E ...` (`-i` for edit in place). You can add `-i.bak` to have it create a `test.config.bak` of the original for you. There is also no need for the single-quote/double-quote scheme. Just double-quote the entire expression e.g. `sed -i -E " ... "` which will allow for variable substitution in the replacement expression. – David C. Rankin Dec 01 '16 at 06:48
  • it prints to the console. but nothing changed in the config.txt file –  Dec 01 '16 at 06:50
  • You are sure you used the `-i` option?? – David C. Rankin Dec 01 '16 at 06:51
  • Glad to hear it. `sed` may take a bit of effort to learn (it does a whole lot more than simple substitutions), but it is worth the effort. Concentrate on mastering the regular expressions and the substitution operation, and you will find it is the Swiss-Army knife of text processing. – David C. Rankin Dec 01 '16 at 06:59
  • To change `DRIVER : FreeTDS` to `DRIVER : ODBC`, you are doing essentially the same thing. You can actually just do `sed -i '/^DRIVER[ ]:/s/FreeTDS/ODBC/'` in that case. The extra `/^stuff/` in `/^stuff/s/.../.../` just tells `sed` to find the line beginning with `stuff` and then make the substitution (without having to use the back-reference). – David C. Rankin Dec 01 '16 at 07:02