如何使用Stream Editor同时替换单词数?

时间:2016-07-14 04:48:56

标签: sed

inputfile:multiple_sed.txt

perl is an interpreterated language

it has three data types

scalar

array

hashes

scalar is a single value

我尝试了以下命令:

sed -e 's/array/scalar/g;s/was/is/g' multiple_sed.txt

输出:

perl is an interpreterated language

it has three data types

scalar

scalar

hashes

scalar is a single value

我尝试了上面的命令来替换两个单词,但是如何更改单词替换的数量(即超过100个单词)。

如何使用流编辑器(sed)来完成它?

1 个答案:

答案 0 :(得分:1)

带有替换文字的单词列表:

<强> wordsfile:

array scalar
is was
interpreterated interpreted

您可以使用process substitution

$ sed -f <(sed 's/^/s\//g;s/ /\//;s/$/\//g' wordsfile) multiple_sed.txt
perl was an interpreted language

it has three data types

scalar

scalar

hashes

scalar was a single value

将内部sed命令传递给-fwordsfile中的所有行都被格式化为sed替换命令。结果将发送到处理multiple_sed.txt中的替换的外部sed。

修改

正如potong所建议的那样,在内部sed命令中使用不同的分隔符使其更具可读性(同时删除了此处无用的g修饰符):

sed -f <(sed 's/^/s#/;s/ /#/;s/$/#/' wordsfile) multiple_sed.txt