Sed 循环遍历文件并替换占位符变量 [英] Sed to loop through file and replace placeholder variables

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

问题描述

我希望使用 bash 循环文件并将所有占位符变量转换为实际变量:

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

  • $PLACEHOLDER_USER -> $USER
  • $PLACEHOLDER_STATE -> $STATE

它需要处理以占位符开头的任何变量并将其转换为实际变量.这是我到目前为止的代码:

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"

但是我不确定如何让它循环遍历整个文件,也不确定如何使用以 $PLACEHOLDER_ 开头的任何变量.

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_.

推荐答案

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

注意这里的引用;双引号允许 shell 用环境中的值替换 $USER$STATE,而反斜杠的美元符号不会被替换.所以 shell 执行了一些替换,当 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 无法访问您的环境变量,但您可以编写一个 shell 脚本,从您的变量中生成一个 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 实例使用 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 脚本将在每个变量名称前加上 $PLACEHOLDER_ 并生成一个 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.)

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

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