【发布时间】:2021-11-27 17:14:17
【问题描述】:
我正在尝试用完整的书面文字形式替换某个文本中的所有数字。
例如,如果输入是
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 的解决方案。
【问题讨论】:
-
$()在解析命令行时被替换,而不是在内部被sed替换。