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 periods as thousands separators using sed. How can I do that?

Sample imput/output:

29371923471239 -> 29.371.923.471.239

share|improve this question

3 Answers 3

From: http://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq4_007.html

sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

echo "981273981723" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' 981,273,981,723

share|improve this answer
1  
An explanation is good for the benefit of your answer, not a reference to a webpage. –  Arcege Feb 26 '12 at 22:32

Try this

echo "29371923471239" | sed -r ':A;s|([0-9]+)([0-9]{3})|\1,\2|g;t A'
29,371,923,471,239

This works by splitting the number into two parts, the last 3 digits and everything else. It then adds a , to the end of everything else and branches back to the start. You can see this working with

echo "29371923471239" | sed -r ':A;s|([0-9]+)([0-9]{3})|\1,\2|g'
29371923471,239
share|improve this answer

USE GHC-ONELINERS FOR A GREAT GOOD

$> echo -n 29371923471239 | ghc -e "import Data.List.Split" -e "putStrLn . reverse . intercalate \".\" . splitEvery 3 . reverse =<< getContents"
29.371.923.471.239

!

share|improve this answer

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.