将给定文本行中的所有数字替换为其货币格式
我想编写一个 Shell(AWK、Sed 也可以)程序来将单行文本作为输入。
其中将任意分布整数字符串。例如
“12884和111933是两个数字,323232也是一个数字”
我希望输出为
“12,884和1,11,933是两个数字,2,23,232也是一个数字”
如果这是PHP,一个简单的preg_replace_callback将服务于目的,但我希望这是在外壳中。 :/
任何指示都会有很大帮助。
I want to write a Shell (AWK, Sed also fine) program to take as a input a SINGLE line of text.
Which will have arbitrarily spread integer strings in it. e.g
"12884 and 111933 are two numbers and 323232 is also a number"
I want the output to be
"12,884 and 1,11,933 are two numbers and 2,23,232 is also a number"
If this was PHP a simple preg_replace_callback would have served the purpose but I want this to be in shell. :/
Any pointers would of great help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有必要使用
tr
来分割行。您可以利用 Bash 的分词功能:我还使用了 globbing 来测试数字序列,而不是依赖于是否会产生错误(取决于它是否是整数)的东西。
It's not necessary to split the line using
tr
. You can make use of Bash's word splitting feature:I've also used globbing to test for a sequence of digits rather than relying on something that creates an error or not depending on whether it's an integer.
我知道这可能又长又难看,但到目前为止这是我能做的最好的事情,我希望它会有所帮助。
遵循解释:
I understand this may be long and ugly but by now is the best I could do, I hope it will help.
Follows the explanation: