通过文件桑达循环和替换占位符变量 [英] Sed to loop through file and replace placeholder variables

查看:258
本文介绍了通过文件桑达循环和替换占位符变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过一个文件来使用bash循环,把所有的占位符varialbes成真正的变量:

I was hoping to use bash to loop through a file and turn all placeholder varialbes into real variables:


  • $ PLACEHOLDER_USER - > $ USER

  • $ PLACEHOLDER_STATE - > $状态

它需要与任何变量开始占位符的工作,并把它变成它的真正变量。这是code我到目前为止有:

It needs to work with any variable starting with placeholder and turn it into its real variable. This is the code I have so far:

$FILE="/mytest.conf"
sed -i "s/$var1/$var2/g" "$FILE"

但是我不知道我怎么撑过整个文件循环,我不知道我怎么能与具有占位符$开头的变量使其_

However I'm not sure how I make it loop through the entire file, and I'm not sure how I can make it with any variable which starts with $PLACEHOLDER_

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

SED 的默认操作是读取和打印输入文件的每一行。您可以通过编写的sed 脚本修改以各种方式这种行为。一个典型的脚本会是这样

The default action of sed is to read and print every line of the input file. You can modify this behavior in various ways by writing a sed script. A typical script would be something like

sed -i "s/\\\$PLACEHOLDER_USER/$USER/g;s/\\\$PLACEHOLDER_STATE/$STATE/g" file

注重这里的报价;双引号允许外壳替换 $ USER $ STATE 从环境的价值,而反斜杠美元标志将不会被取代。所以外壳进行一些替换,并通过 SED 实际运行时间,剧本已经成为

Pay attention to the quoting here; the double quotes allow the shell to replace $USER and $STATE with their values from the environment, while the backslashed dollar signs will not be substituted. So the shell performs some substitutions, and by the time sed actually runs, the script has become

sed -i 's/\$PLACEHOLDER_USER/fred/g;s/\$PLACEHOLDER_STATE/Arkansas/g' file

(我这里提供的单引号强调的是,没有进一步的替代会发生。)

(I supplied single quotes here to emphasize that no further substitution will take place.)

在更一般的情况下, SED 有你的环境变量没有进入,但你可以写一个生成一个 SED 从变量的脚本。

In the more general case, sed has no access to your environment variables, but you can write a shell script which generates a sed script from your variables.

env | sed 's%^\([^=]*\)=\(.*\)%s/\\\$PLACEHOLDER_\1/\2/g%' | sed -f - file

这是有点棘手。从第一个 SED 脚本的输出是由另一个 SED <读取另一个 SED 脚本/ code>实例 SED -f - 。 (这是不支持的所有平台上,而应该在Linux上至少工作。如果你不支持这一点,你可以通过编写脚本到一个临时文件,或者使用命令替换解决它。)

This is somewhat tricky. The output from the first sed script is another sed script which is read by another sed instance with sed -f -. (This is not supported on all platforms, but should at least work on Linux. If yours does not support this, you can work around it by writing the script to a temporary file, or using a command substitution.)

ENV 列出你的环境变量。第一个 SED 脚本将preFIX与每个变量名\\ $占位符_ 并生成<$ C的一个片段$ C>的sed 从环境的值来代替的,任何发生。 (警告:如果该值包含正则表达式元字符,你需要一个显著更为复杂的脚本如果值可以包含斜线,您需要使用另一个分隔符来代替斜杠分隔符,你可以使用任何你喜欢的ASCII字符。)

env lists your environment variables. The first sed script will prefix each variable name with \$PLACEHOLDER_ and generate a snippet of sed to replace any occurrence of that with its value from the environment. (Caveat: If the value contains regex metacharacters, you will need a significantly more complex script. If the values can contain slashes, you need to replace the slash delimiter with another delimiter; you can use any ASCII character you like.)

这篇关于通过文件桑达循环和替换占位符变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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