我想用一组不同的预定义字符串替换多个字符串模式。
例如:
输入:
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)
所以在这里我想转换为以下模式。
让我知道是否可以完成。
如果您的数据位于文件中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)