我有一個隨機包含一些數字的文本,我想找到它們並將數千個分開。如何在bash中插入千位分隔符?
輸入
Some random text 123456 and other text 987654 etc.
輸出
Some random text 123 456 and other text 987 654 etc.
任何幫助,將不勝感激。
我有一個隨機包含一些數字的文本,我想找到它們並將數千個分開。如何在bash中插入千位分隔符?
輸入
Some random text 123456 and other text 987654 etc.
輸出
Some random text 123 456 and other text 987 654 etc.
任何幫助,將不勝感激。
該任務有一些特殊情況。我爲整數的第一次嘗試只將這個遞歸GNU sed
命令:
sed -r ':a;s/([0-9])([0-9]{3}([^0-9]|$))/\1 \2/;ta'
演示:
$ echo 'Some random text 123456 and other text 9876522224
5464, 83478234 and 2312.' | sed -r ':a;s/([0-9])([0-9]{3}([^0-9]|$))/\1 \2/;ta'
Some random text 123 456 and other text 9 876 522 224
5 464, 83 478 234 and 2 312.
適合我,謝謝 – user219882 2013-05-03 22:08:12
很好地完成;以下同時適用於GNU和BSD(OSX)sed:'sed -E -e':a'-e's /([[:digit:]])([[:digit:]] {3}([^[:digit:]] | $))/ \ 1 \ 2 /; ta'' – mklement0 2015-06-11 17:25:44
符合POSIX的解決方案是可能的,但很麻煩,因爲POSIX Sed僅支持BRE(基本正則表達式),而POSIX BRE不支持交替('|');基本上,你必須通過複製正則表達式和改變結尾來處理交替:'sed -e':a'-e's/\([[:digit:]] \)\([[:digit:]] \ {\ \ \ \ \ \ \ \(\ [[:digit:]] \)\([[:digit :]] \ {3 \} $ \)/ \ 1 \ 2 /; ta'' – mklement0 2015-06-11 17:36:43
請問一個Perl解決方案的工作? – canhazbits 2013-05-03 21:58:24
你有十進制數嗎? – Kent 2013-05-03 22:06:03
可能重複[數字格式在BASH與千位分隔符](http://stackoverflow.com/questions/9374868/number-formatting-in-bash-with-thousand-separator) – 2013-05-03 22:13:09