使用Sed扩展文件内的环境变量 [英] Using Sed to expand environment variables inside files

查看:213
本文介绍了使用Sed扩展文件内的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Sed来扩展文件中的变量。

I'd like to use Sed to expand variables inside a file.

假设我导出了一个变量VARIABLE = something,并且有一个test文件,以下:

Suppose I exported a variable VARIABLE=something, and have a "test" file with the following:

I'd like to expand this: "${VARIABLE}"

我一直在尝试以下命令,但无效:

I've been trying commands like the following, but to no avail:

cat test | sed -e "s/\(\${[A-Z]*}\)/`eval "echo '\1'"`/" > outputfile

结果是outputfile,变量仍未展开:

The result is the "outputfile" with the variable still not expanded:

I'd like to expand this: "${VARIABLE}"

但是,在bash控制台中运行 evalecho'$ {VARIABLE}'会导致值回显 ,我测试过,并且这种模式是完全匹配的。

Still, running eval "echo '${VARIABLE}' in bash console results in the value "something" being echoed. Also, I tested and that pattern is trully being matched.

所需的输出将是

I'd like to expand this: "something"

任何人都可以散发光在这个?

Can anyone shed a light on this?

推荐答案

考虑你的试用版本:

cat test | sed -e "s/\(\${[A-Z]*}\)/`eval "echo '\1'"`/" > outputfile

这不行的原因是因为它需要shell的部分预测,sed脚本在任何模式与sed匹配之前生成,因此shell无法执行该作业对你来说。

The reason this doesn't work is because it requires prescience on the part of the shell. The sed script is generated before any pattern is matched by sed, so the shell cannot do that job for you.

过去我已经做了几个方法。通常,我已经有一个已知变量及其值的列表,我已经从该列表中替换:

I've done this a couple of ways in the past. Normally, I've had a list of known variables and their values, and I've done the substitution from that list:

for var in PATH VARIABLE USERNAME
do
    echo 's%${'"$var"'}%'$(eval echo "\$$var")'%g'
done > sed.script

cat test | sed -f sed.script > outputfile

如果要任意映射变量,那么您需要处理整个环境的变量名称的固定列表,使用 env ,适当编辑的输出),或者使用Perl或Python。

If you want to map variables arbitrarily, then you either need to deal with the whole environment (instead of the fixed list of variable names, use the output from env, appropriately edited), or use Perl or Python instead.

请注意,如果环境变量的值在版本中包含斜杠,则会在s ///符号中使用斜杠作为字段分隔符遇到问题。我使用'%',因为相对较少的环境变量使用它 - 但是在一些机器上有一些包含'%'字符,所以一个完整的解决方案是棘手的。您还需要担心值中的反斜杠。你可能需要使用像'$ code $ $(eval echo\ $$ var| sed's / [\%] / \\& / g' $ c>'来转义环境变量值中的反斜杠和百分比符号。最终的皱纹:某些版本的 sed 具有(或已经)脚本大小有限的容量 - 旧版本的HP-UX的限制约为100.我不是确定这是否仍然是一个问题,但最近是5年前。

Note that if the value of an environment variable contains a slash in your version, you'd run into problems using the slash as the field separator in the s/// notation. I used the '%' since relatively few environment variables use that - but there are some found on some machines that do contain '%' characters and so a complete solution is trickier. You also need to worry about backslashes in the value. You probably have to use something like '$(eval echo "\$$var" | sed 's/[\%]/\\&/g')' to escape the backslashes and percent symbols in the value of the environment variable. Final wrinkle: some versions of sed have (or had) a limited capacity for the script size - older versions of HP-UX had a limit of about 100. I'm not sure whether that is still an issue, but it was as recently as 5 years ago.

原始脚本的简单的改编如下:

The simple-minded adaptation of the original script reads:

env |
sed 's/=.*//' |
while read var
do
    echo 's%${'"$var"'}%'$(eval echo "\$$var" | sed 's/[\%]/\\&/g')'%g'
done > sed.script

cat test | sed -f sed.script > outputfile

但是,更好的解决方案使用的事实是,您已经在 env ,所以我们可以写:

However, a better solution uses the fact that you already have the values in the output from env, so we can write:

env |
sed 's/[\%]/\\&/g;s/\([^=]*\)=\(.*\)/s%${\1}%\2%/' > sed.script
cat test | sed -f sed.script > outputfile

这是更安全的,因为shell从不评估任何不应该评估的东西 - 你必须在变量值中小心shell元字符。我认为,如果 env 的某些输出格式错误,这个版本只会遇到麻烦。

This is altogether safer because the shell never evaluates anything that should not be evaluated - you have to be so careful with shell metacharacters in variable values. This version can only possibly run into any trouble if some output from env is malformed, I think.

当心 - 写 sed 脚本与 sed 是一个深奥的职业,但一个说明了好的工具的力量。

Beware - writing sed scripts with sed is an esoteric occupation, but one that illustrates the power of good tools.

所有这些示例都没有清理临时文件。

All these examples are remiss in not cleaning up the temporary file(s).

这篇关于使用Sed扩展文件内的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆