SED 替换变量

发布于 2024-09-19 00:13:26 字数 675 浏览 5 评论 0原文

我有一个名为 check.txt 的文件,其中包含以下内容:

$ cat check.txt
~/bin/tibemsadmin -server $URL-user $USER -password $PASWRD
$

我有一个主脚本,其中 $URL、$USER、$PASWRD 的值是从主脚本获取的。我想使用 SED 实用程序将 $URL、$USER、$PASWRD 替换为 check.txt 中的实际值。

我正在尝试这样,但失败了。

emsurl=tcp://myserver:3243
emsuser=test
emspasswd=new
sed s/$URL/${emsurl}/g check.txt >> check_new.txt
sed s/$USER/${emsuser}/g check.txt_new.txt >> check_new_1.txt
sed s/PASWRD/${emspasswd}/g check_new_1.txt >> final.txt

我的 Final.txt 输出如下所示:

~/bin/tibemsadmin -server tcp://myserver:3243 -user test -password new

你能帮我吗?

I have a file named check.txt which has the below contents:

$ cat check.txt
~/bin/tibemsadmin -server $URL-user $USER -password $PASWRD
$

I have a main script where the values of $URL, $USER, $PASWRD are obtained from the main script. I want to use the SED utility to replace the $URL, $USER, $PASWRD to the actual values in the check.txt.

I am trying like this but it fails.

emsurl=tcp://myserver:3243
emsuser=test
emspasswd=new
sed s/$URL/${emsurl}/g check.txt >> check_new.txt
sed s/$USER/${emsuser}/g check.txt_new.txt >> check_new_1.txt
sed s/PASWRD/${emspasswd}/g check_new_1.txt >> final.txt

My final.txt output is desired as below:

~/bin/tibemsadmin -server tcp://myserver:3243 -user test -password new

Could you please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2024-09-26 00:13:27

您必须非常小心地使用引号。您还需要学习如何在一次传递中执行多个操作,和/或如何使用管道。

emsurl=tcp://myserver:3243
emsuser=test
emspasswd=new
sed -e "s%\$URL%${emsurl}%g" \
    -e "s%\$USER%${emsuser}%g" \
    -e "s%\$PASWRD%${emspasswd}%g" check.txt >final.txt

您的问题是 shell 在命令行中扩展了“$URL”(可能什么都没有),这意味着 sed 看到的内容与您想要的内容不同。通过使用 \ 转义 $sed 可以看到您想要的内容。

请注意,我最初在替换操作中使用 / 作为分隔符;然而,正如 DarkDust 正确指出的那样,这是行不通的,因为 URL 中有斜杠。我的正常后备字符是 % - 如现在所示 - 但它可能出现在某些 URL 中,并且可能不合适。如果我需要担心这个问题,我可能会使用控制字符,例如 control-A - 或者我会使用 Perl,它可以在玩游戏时不会感到困惑。

您还可以将三个单独的 -e 表达式合并为一个,并用分号替换它们。然而,我更喜欢将三个操作清晰地分开。

You have to be rather careful with your use of quotes. You also need to learn how to do multiple operations in a single pass, and/or how to use pipes.

emsurl=tcp://myserver:3243
emsuser=test
emspasswd=new
sed -e "s%\$URL%${emsurl}%g" \
    -e "s%\$USER%${emsuser}%g" \
    -e "s%\$PASWRD%${emspasswd}%g" check.txt >final.txt

Your problem is that the shell expanded the '$URL' in your command line (probably to nothing), meaning that sed got to see something other than what you intended. By escaping the $ with the \, sed gets to see what you intended.

Note that I initially used / as the separator in the substitute operations; however, as DarkDust rightly points out, that won't work since there are slashes in the URLs. My normal fallback character is % - as now shown - but that can appear in some URLs and might not be appropriate. I'd probably use a control character, such as control-A, if I needed to worry about that - or I'd use Perl which would be able to play without getting confused.

You can also combine the three separate -e expressions into one with semi-colons replacing them. However, I prefer the clarity of the three operations clearly separated.

世界如花海般美丽 2024-09-26 00:13:27

您可以通过修改主脚本来采取稍微不同的方法,如下所示:-

export URL="tcp://myserver:3243"
export USER=test
export PASWRD=new
. ./check.txt

这将设置变量,然后在主脚本的上下文中运行 check.txt

You could take a slightly different approach by modifying your main script as follows :-

export URL="tcp://myserver:3243"
export USER=test
export PASWRD=new
. ./check.txt

This sets up the variables and then runs check.txt within the context of your main script

内心激荡 2024-09-26 00:13:27

虽然你没有说失败是什么,但我想我看到了问题。

我建议你这样做:

sed "s|\$URL|${emsurl}|g"

也就是说,第一个 $ 需要转义,因为你想要它的字面意思。然后,我建议您使用 | (管道)作为分隔符,而不是 /,因为它不在您的字符串中使用。最后,使用 " 确保 shell 将内容解释为字符串。

然后您可以将所有内容通过管道连接在一起,而不需要任何临时文件:

sed "s|\$URL|${emsurl}|g" | sed "s|\$USER|${emsuser}|g" | sed "s|\$PASSWRD|${emspasswd}|g"

Although you don't say what's failing I guess I see the problems.

I suggest you do this:

sed "s|\$URL|${emsurl}|g"

That is, the first $ needs to be escaped because you want it literally. Then, instead of / I suggest you use | (pipe) as delimiter since it's not used in your strings. Finally, use " to ensure the content is interpreted as string by the shell.

You can then pipe everything together to not need any temporary files:

sed "s|\$URL|${emsurl}|g" | sed "s|\$USER|${emsuser}|g" | sed "s|\$PASSWRD|${emspasswd}|g"
分開簡單 2024-09-26 00:13:27

变量替换应该在 sed 表达式之外,并且 '$' 应该被转义;在你的情况下是这样的:

sed -e 's/\$URL/'$emsurl'/g' -e 's/\$USER/'$emsuser'/g' -e 's/\$PASSWORD/'$emaspasswd'/g'

无论如何,在你的地方,我会避免使用 $ 来匹配模板文件中的占位符,因为它会导致与 BASH 变量的混淆,而是使用不同的模式(例如@URL@)。

Variable substitution should be outside sed expression and '$' should be escaped; in your case something like this:

sed -e 's/\$URL/'$emsurl'/g' -e 's/\$USER/'$emsuser'/g' -e 's/\$PASSWORD/'$emaspasswd'/g'

Anyway in your place I would avoid using $ to match placeholders in a template file, because it's causing confusion with BASH variables, use a different pattern instead (for instance @URL@).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文