Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .