Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to insert thousand separator into a number sequence with sed and this ugly RE sed -e :a -e 's/\(.*[0-9]\)\(\([0-9]\{3\}\)\(,[0-9]\{3\}\)*\(\.[0-9]*\)\)/\1,\2/;ta' [filename].

Is there a more elegant solution?

===========================================================

EDIT

One thing I omitted just now: the number may have a decimal point. Give an example:

123456.7890

I want the result after separating 123,456.7890

Thanks and best regards.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

It's not using sed, but you could try something similar to this:

echo "10000" | xargs printf "%'d\n"

Or a solution using sed from here:

echo "2312341" | sed -r ':L;s=\b([0-9]+)([0-9]{3})\b=\1,\2=g;t L'

share|improve this answer
    
Thanks in advance. The printf works fine. while the sed version seems cannot handle decimal point correctly. –  Summer_More_More_Tea Feb 21 '12 at 14:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.