【问题标题】:Reading variable in sed from forloop从 for 循环中读取 sed 中的变量
【发布时间】:2017-02-20 13:01:45
【问题描述】:

我正在尝试使用 sed 读取文件并更改文件中的变量,但 sed 不读取变量的值。

这里我想用market 替换@@ 并将输出放在另一个文件中

markets=au,uk,ca

IFS="," read -ra items <<<"$markets"

for item in "${items[@]}"
do
    echo "country is :: $item"
    sed -i 's/@@/${!item}/g' sample_guid >> /test/eric/Guid_del
done

【问题讨论】:

  • 你一直使用的@@是什么?
  • 查看输入示例之前/之后会很有用...使用-i 意味着不会有任何标准输出...对于变量替换,需要use double quotes
  • 当您有一个带有@@ 的模板文件sample_guid 将被不同运行的市场替换时,请尝试删除sed 的-i 选项。

标签: arrays shell unix sed


【解决方案1】:

当 sample_guid 看起来像 这是市场@@ 的示例。我想在这里填补市场。
如果您想针对不同的市场进行此操作, 通过额外的参数使用 printf 循环的特性:

printf "This is an example for market %s. I want the market filled in here.\n" au uk ca

当sample_guid 真的只有一个@@ 时,可以将printf 解决方案扩展为

printf "$(sed 's/@@/%s/' sample_guid)\n" $(tr "," " " <<< "${markets}")

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 2020-02-21
    • 1970-01-01
    • 2016-07-07
    • 2017-04-01
    • 1970-01-01
    • 2022-09-25
    • 2017-09-07
    • 2017-12-09
    相关资源
    最近更新 更多