用不同的映射字符串集替换多个字符串

use*_*008 5 sed replace

我想用一组不同的预定义字符串替换多个字符串模式。

例如:

输入:

This sentence will be converted to something different. This sentence can contain same words.
Run Code Online (Sandbox Code Playgroud)

输出:

These words need to be replaced with something different. These words can contain same alphabets. 
Run Code Online (Sandbox Code Playgroud)

所以在这里我想转换为以下模式。

  • 这 => 这些
  • 句子 => 词
  • 将 => 需要
  • 转换=>替换
  • 到 => 与
  • 单词 => 字母

让我知道是否可以完成。

iga*_*gal 3

如果您的数据位于文件中data.txt(例如),那么您可以sed在 for 循环内使用。也许是这样的:

replacements=(
    This:These
    sentence:word
    will:need to
    converted:repalced
    to:with
    words:alphabets
)

for row in "${replacements[@]}"; do
    original="$(echo $row | cut -d: -f1)";
    new="$(echo $row | cut -d: -f2)";
    sed -i -e "s/${original}/${new}/g" data.txt;
done
Run Code Online (Sandbox Code Playgroud)