用脚本的输出替换捕获组

Replace a capture group with the output from a script

我正在尝试用完整的文字形式替换特定文本中的所有数字。

例如,如果输入是

this university has 9123 students
one of its departments has 2315 students

输出将是

this university has nine thousand one hundred and twenty three students
one of its departments has two thousand three hundred and fifteen students

我有一个名为 num2words.sh 的脚本,它可以将 9123 这样的数字转换为单词形式。我想使用此脚本替换名为 file1.txt 的文件中的所有数字。我一直在尝试使用 sed 但我无法让它工作。到目前为止我有这个,

cat file1.txt | sed -e "s/\([0-9]\+\)/)/$(./num2words.sh )"

我想 \1 没有被正确传递。谁能为此提供解决方案?使用 sed 会更好,但我也愿意接受 awkperl 的解决方案。

使用 perl,

perl -pe 's#[0-9]+#`./num2words.sh $&`#eg' file1.txt

这可能对你有用 (GNU sed):

sed -E 's#[0-9]+#$(./num2words.sh &)#g;T;s/.*/echo "&"/e' file

将数字放入 $(./num2words.sh x) 其中 x 是一个数字。

如果没有替代品就退出。

将整行双引号括起来,然后使用 e 标志将其回显以评估模式 space.

N.B。可能会有不需要的副作用。