SED 替换变量
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须非常小心地使用引号。您还需要学习如何在一次传递中执行多个操作,和/或如何使用管道。
您的问题是 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.
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.您可以通过修改主脚本来采取稍微不同的方法,如下所示:-
这将设置变量,然后在主脚本的上下文中运行 check.txt
You could take a slightly different approach by modifying your main script as follows :-
This sets up the variables and then runs check.txt within the context of your main script
虽然你没有说失败是什么,但我想我看到了问题。
我建议你这样做:
也就是说,第一个
$
需要转义,因为你想要它的字面意思。然后,我建议您使用|
(管道)作为分隔符,而不是/
,因为它不在您的字符串中使用。最后,使用"
确保 shell 将内容解释为字符串。然后您可以将所有内容通过管道连接在一起,而不需要任何临时文件:
Although you don't say what's failing I guess I see the problems.
I suggest you do this:
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 表达式之外,并且 '$' 应该被转义;在你的情况下是这样的:
无论如何,在你的地方,我会避免使用 $ 来匹配模板文件中的占位符,因为它会导致与 BASH 变量的混淆,而是使用不同的模式(例如@URL@)。
Variable substitution should be outside sed expression and '$' should be escaped; in your case something like this:
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@).