Quote:
Originally posted by Ygor
# add commas to numeric strings, changing "1234567" to "1,234,567"
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
That's pretty awesome. But it's more powerful than the comments seem to indicate. It handles leading minus signs. It also handles a decimal point followed by up to three decimal places. It not obvious how to handle commas with more than three decimal places. Leave them alone? Like this: "1,234,567,890.0000" ?
If that is right, we can extend the sed one-liner:
sed -e :a -e 's/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
This countinues to handle leading minus, plus, and dollar signs. In the accounting world, I've seen an odd convention of putting numbers in parentheses. That also works.