linux - 用脚本的输出替换捕获组
问题描述
我正在尝试用完整的书面文字形式替换某个文本中的所有数字。
例如,如果输入是
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
。
解决方案
使用 perl,
perl -pe 's#[0-9]+#`./num2words.sh $&`#eg' file1.txt
推荐阅读
- linux - 如何从 tty 读取串行输入并将其写入 Linux 中的文件?
- php - PHP 简码产生意想不到的结果
- java - Junit 5 的 assertEquals 精度为 double
- java - Selenium - Java:有没有办法使用 WebElement 而不是按类获取定位器
- python - 有没有我可以在 python 中使用的函数来实现 Matlab 函数 qtdecomp?
- dns - 为什么域名中的资源(例如hello.jpg)访问无效,但IP地址有效
- ios - Swift:如何在返回类型为 self 的协议中声明 func?
- html - CSS绝对位置子菜单在父容器上显示滚动条
- php - 我无法在我的自定义插件 php 类中挂接到 wpcf7_mail_sent
- javascript - 挂载方法 - Vue.js