如果使用Shell脚本匹配字符串的一部分,则替换行

用户名

我有一个config.txt包含这样的文件调用

[MS-SQL]
DRIVER : FreeTDS
SERVER : 138.23.21.45

我需要做的是,无论之后SERVER :需要包含什么字符串值,都需要用shell变量(如)中的内容替换$SERVER_IP

最后config.ini需要像这样。(考虑bash shell变量包含这样的内容$SERVER_IP=192.168.5.3

[MS-SQL]
DRIVER : FreeTDS
SERVER : 192.168.5.3
以撒

这将改变这一行:

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

描述:

-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.

如果需要,也可以在之后更改值DRIVER

sed -iE 's/(DRIVER : )(.*)/\1ODBC/' config.txt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章