Place , character after 3 digits from left to right in a string

Hi All,

Could anyone please help me, how to put �,' character after 3 digits from right to left count,among 17 digits sting. unix scripting
Example - I am having 12345678911234567 digits
Accepted result-- 12,345,678,911,234,567
Note- 12345678911234567 digits will be dynamic at run time, I don't know how many digit will generate may be less or more .
please help me
Thanks,
Krupa

If you have 'rev' from util-linux package , it's easy:

$ echo 12345678911234567 | rev | sed 's/\([0-9][0-9][0-9]\)/\1,/g' | rev
12,345,678,911,234,567

Try Perl. Regex below picked from Perl Cookbook, section 2.17.

[user@ubuntu dir]# echo 12345678911234567 | perl -lne 'chomp; $x=reverse; $x=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; print (scalar reverse $x)'
12,345,678,911,234,567
[user@ubuntu dir]#

This takes care of putting commas in float numbers like 123456.789 etc.

recent bash/ksh93:

printf "%'d\n" 12345678911234567
1 Like

Scrutinizer,
Could you please explain ?

Hi pravin,

The ' -flag is not in the POSIX specification of the printf utility itself:printf utility: extended description, but recent bash and ksh93 appear to support this part of the POSIX specification of the printf function syntax:

It depends on locale so you may need to prepend it with the desired locale.

LC_NUMERIC=en_US.UTF-8 printf "%'d\n" 12345678911234567
4 Likes
echo 12345678911234567 | rev | sed 's/\(...\)/\1,/g' | rev

Hi Mirni,

echo 123456 | rev | sed 's/\([0-9][0-9][0-9]\)/\1,/g' | rev

result -

,123,456

but i don't need , comma from Begin it should be either Zero(0) or null (nothing).

Please help me ...

Thanks,
Krupa

Hmmm... I'd add another command to sed:

echo 123456 | rev | sed -e 's/\(...\)/\1,/g' -e 's/,$//' | rev

Alternatively, you could do:

echo 123456 | rev | sed -e 's/\(...\)\(.\)/\1,\2/g' | rev
1 Like

This is a very interesting problem. Perhaps the best (=simpliest) solution is the one from Scrutinizer and it is quite idle musing to try something in one tool (sed) which could be done a lot easier in another. Still, just to satisfy my curiosity:

Several posters here have suggested variants of the following algorithm: replace every three characters with these characters plus an appended comma. Reverse before and after this:

echo <number> | rev | sed -e 's/\(...\)/\1,/g' | rev

This may work but it is interesting to try to solve it in sed alone. It is possible to set up a loop appending the rightmost character to holdspace until the pattern space is empty. This would effectively do the same as the rev utility.

But there is a much simplier way to achieve our goal. Let us first apply our algorithm without using the rev utility. The outcome, depending on the length of the number, would be one of the following three forms:

1234   -> 123,4
12345  -> 123,45
123456 -> 123,456,

We can correct the first one by moving all the commata 2 places leftwards. The second variant can be corrected by moving the commata 1 place leftwards and the last line just has one superfluous comma in the end. Therefore:

echo 12345678901234 |\
sed 's/\(...\)/\1,/g
     /,.$/ {
          s/\(..\),/,\1/g
          }
     /,..$/ {
          s/\(.\),/,\1/g
          }
     s/,$//'

Which will do the trick in sed alone. My curiosity is satisfied indeed.

bakunin

2 Likes

Hi.

Adapted code from perl FAQ in shell functions for single string and for a file (does not need rev):

#!/usr/bin/env bash

# @(#) s2	Demonstrate commify a numeric string, perl in shell # functions.
# See: perldoc -q commas

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
cs() { echo "$1" | perl -wp -e '1 while s/^([-+]?\d+)(\d{3})/$1,$2/; ' ; }
cf() { perl -wp -e '1 while s/^([-+]?\d+)(\d{3})/$1,$2/; ' "$1" ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=${1-data1}

pl " With single string:"
for number in 1 12 123 1234 12345 123456 -1 -12 -123 -1234 +1 +12 +123 +1234
do
  pe " for $number, result is $( cs $number )"
done

pl " With contents of a file:"
head $FILE
pe
cf $FILE

exit 0

producing:

% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 With single string:
 for 1, result is 1
 for 12, result is 12
 for 123, result is 123
 for 1234, result is 1,234
 for 12345, result is 12,345
 for 123456, result is 123,456
 for -1, result is -1
 for -12, result is -12
 for -123, result is -123
 for -1234, result is -1,234
 for +1, result is +1
 for +12, result is +12
 for +123, result is +123
 for +1234, result is +1,234

-----
 With contents of a file:
999
1000
-1000
+1000
12345
1234567.1234
-1234567.1234
+1234567.1234
12345678911234567

999
1,000
-1,000
+1,000
12,345
1,234,567.1234
-1,234,567.1234
+1,234,567.1234
12,345,678,911,234,567

Best wishes ... cheers, drl

using python 3.2:-

import re

x = '12345678911234567'
m = re.findall(r"(\d{2,3})",x[::-1])
y = ",".join(m)
result = y[::-1]
print(result)
output:

'12,345,678,911,234,567'

BR

;);):D:D

Some awks (gawk, mawk, BSD awk) can do this:

echo 12345678911234567 | awk '{for(i=NF-3;i>=1;i-=3)$i=$i","}1' FS= OFS=

actually if you want the number to be dynamic , my code will be better if you do below:-

import re

x = '12345678911234567'
m = re.findall(r"(\d{1,3})",x[::-1])
y = ",".join(m)
result = y[::-1]
print(result)

in this way , any number you give with even or odd count will give you a correct result.
BR
:slight_smile: