user219882
user219882

Reputation: 15844

How to insert thousand separator with in bash?

I have a text which randomly contains some numbers and I want to find them and separate thousands.

Input

Some random text 123456 and other text 987654 etc.

Output

Some random text 123 456 and other text 987 654 etc.

Any help would be appreciated.

Upvotes: 0

Views: 611

Answers (1)

Lev Levitsky
Lev Levitsky

Reputation: 65851

The task has some corner cases. My first attempt for integers only would be this recursive GNU sed command:

sed -r ':a;s/([0-9])([0-9]{3}([^0-9]|$))/\1 \2/;ta'

Demo:

$ 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.

Upvotes: 3

Related Questions