Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add commas to numeric strings in Unix

Tags:

linux

shell

unix

Add commas to numbers in Unix, need to change 1234567 as 1,234,567

Is it possible in sed or awk? If so please give some sample.

like image 375
Marjer Avatar asked Oct 25 '25 17:10

Marjer


1 Answers

$ LC_NUMERIC=en_US

$ printf "%'.f" 1234567
1,234,567

Courtesy of jim mcnamara: Locale determines what happens with the thousands separator.

$ export LC_NUMERIC="en_US.UTF-8"
$ printf "%'f\n" 1234567.777
1,234,567.777000

$ export LC_NUMERIC=C
$ printf "%'f\n" 1234567.777
1234567.777000

This last one has no commas. Your result had no commas. Do you see why now? Locale LC_NUMERIC setting is your answer. The C locale does not have a thousands separator. I'm guessing your locale does not either.

like image 177
4 revs, 2 users 96%Steven Penny Avatar answered Oct 28 '25 07:10

4 revs, 2 users 96%Steven Penny