如何使用sed来改变我的配置文件

如何使用sed来改变我的配置文件

本文介绍了如何使用sed来改变我的配置文件,具有灵活键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要搜索一个配置文件,这个前pression:central.database。
那么我想改变与central.database到SQLtest上。

相关的设置

配置文件的布局是这样开始:

  central.database = SQLFIRSTTEST

这是我希望它看起来像sed的更换后:

  central.database = SQLtest上

我在bash脚本这样做,有什么建​​议,建议或替代解决方案,欢迎!

(事实上两者 central.database SQLtest上从这里来的bash变量。)


我目前的code(第三次):

  sshRetValue = $(SSH -p35903-i $ HOME / sshids / idrsa-1.old $ {1}<< EOF
                SED -iS / ^ \\($ CENTRAL_DB_N​​AME \\ S * = \\ s * \\)* \\ $ / \\ $ 1 CENTRAL_DB_VALUE /。/home/testing.txt;
                回声$?
        EOF
        )

错误消息:

 伪终端不会被分配,因为标准输入不是终端。
SED:-e前pression#1,烧焦58:未知的选项'S'
-bash:3号线:EOF:命令未找到


解决方案

下面是一个例子EX pression:

  SED -i的/ ^ \\(中央\\ .database \\ S * = \\ S * \\)。* $ / \\ 1SQLTEST /'file.cfg

如果你想匹配其与 / 的东西,你可以使用另一个分隔符:

  SED -i的#^ \\(美分/ RAL \\。数据/基\\ S * = \\ S * \\)。* $#\\ 1SQL / TEST#'文件。 CFG

或用变量扩展:

  VAL =SQLtest上
SED -iS / ^ \\(中央\\ .database \\ S * = \\ S * \\)。* \\ $ / \\ 1 $ VAL /file.cfg

在您的例子:

  sshRetValue =`sed的-iS / ^ \\(\\ $ 1 CENTRAL_DB_N​​AME \\ S * = \\ S * \\)。* \\ $ / \\ $ 1 CENTRAL_DB_VALUE //家/ testing.txt`;

有一个\\ $ 1 CENTRAL_DB_N​​AME之前是无效的。此外,sed的不打印它的返回值。这是preferred的检查方法的返回值:

  SED -iS / ^ \\($ CENTRAL_DB_N​​AME \\ S * = \\ s * \\)* \\ $ / \\ $ 1 CENTRAL_DB_VALUE /。/home/testing.txt;
sed_return_value = $?

和最终输送至SSH(未测试):

  sed_return_value = $(SSH服务器16;< EOF
    SED -iS / ^ \\($ CENTRAL_DB_N​​AME \\ S * = \\ s * \\)* \\ $ / \\ $ 1 CENTRAL_DB_VALUE /。/home/testing.txt;
    回声$?
EOF

该-i是在输入文件替换数据。否则SED写到stdout。

正前pressions有一个属于自己的领域。这将是无法解释它们的深度在计算器的答案,除非有躲避的你一些特定的功能。

I want to search a configuration file for this expression: "central.database".I then want to change the setting associated with "central.database" to "SQLTEST".

The layout of the config file would look like this initially:

central.database = SQLFIRSTTEST

This is what i want it to look like after the sed replacement:

central.database = SQLTEST

I am doing this in a bash script, any suggestions, recommendations or alternative solutions are welcome!

(Actually both central.database and SQLTEST come from bash variables here.)


My current code (third attempt):

        sshRetValue=$(ssh -p "35903" -i $HOME/sshids/idrsa-1.old ${1} <<EOF
                sed -i "s/^\($CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt;
                echo $?
        EOF
        )

Error message:

Pseudo-terminal will not be allocated because stdin is not a terminal.
sed: -e expression #1, char 58: unknown option to `s'
-bash: line 3: EOF: command not found
解决方案

Here's an example expression:

sed -i 's/^\(central\.database\s*=\s*\).*$/\1SQLTEST/' file.cfg

If you want to match stuff with / in it, you can use another delimiter:

sed -i 's#^\(cent/ral\.data/base\s*=\s*\).*$#\1SQL/TEST#' file.cfg

Or with variable expansion:

VAL="SQLTEST"
sed -i "s/^\(central\.database\s*=\s*\).*\$/\1$VAL/" file.cfg

In your example:

sshRetValue=`sed -i "s/^\(\1$CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt`;

There's a \1 before $CENTRAL_DB_NAME that's invalid. Also, sed doesn't print it's return value. This is the preferred way to check return values:

sed -i "s/^\($CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt;
sed_return_value=$?

And ultimately piping to ssh (not tested):

sed_return_value=$(ssh server <<EOF
    sed -i "s/^\($CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt;
    echo $?
EOF
)

The -i is for replacing data in the input file. Otherwise sed writes to stdout.

Regular expressions are a field of their own. It would be impossible to explain them in depth in a stackoverflow answer, unless there is some specific function that's eluding you.

这篇关于如何使用sed来改变我的配置文件,具有灵活键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:12