首页 > 解决方案 > 用脚本的输出替换捕获组

问题描述

我正在尝试用完整的书面文字形式替换某个文本中的所有数字。

例如,如果输入是

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)"

我猜\1没有被正确传递。任何人都可以为此提供解决方案吗?使用sed会更好,但我也愿意接受解决awk方案perl

标签: linuxstringbashsed

解决方案


使用 perl,

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

推荐阅读