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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Place , character after 3 digits from left to right in a string
# 1  
Old 05-12-2012
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
# 2  
Old 05-12-2012
If you have 'rev' from util-linux package , it's easy:

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

# 3  
Old 05-12-2012
Try Perl. Regex below picked from Perl Cookbook, section 2.17.

Code:
[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.
# 4  
Old 05-12-2012
recent bash/ksh93:
Code:
printf "%'d\n" 12345678911234567


Last edited by Scrutinizer; 05-12-2012 at 07:27 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-13-2012
Scrutinizer,
Could you please explain ?
# 6  
Old 05-13-2012
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:

Quote:
The flag characters and their meanings are:

'
[XSI] The integer portion of the result of a decimal conversion ( %i, %d, %u, %f, %F, %g, or %G ) shall be formatted with thousands' grouping characters. For other conversions the behavior is undefined. The non-monetary grouping character is used.
It depends on locale so you may need to prepend it with the desired locale.
Code:
LC_NUMERIC=en_US.UTF-8 printf "%'d\n" 12345678911234567


Last edited by Scrutinizer; 05-13-2012 at 06:32 AM..
These 4 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-13-2012
Code:
echo 12345678911234567 | rev | sed 's/\(...\)/\1,/g' | rev

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract n-digits from string in perl

Hello, I have a log file with logs such as 01/05/2017 10:23:41 : file.log.38: database error, MODE=SINGLE, LEVEL=critical, STATE: 01170255 (mode main how can i use perl to extract the 8-digit number below from the string 01170255 Thanks (7 Replies)
Discussion started by: james2009
7 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. UNIX for Dummies Questions & Answers

How to search for a file having a particular character in a particular place in a directory.?

Hi Guys, I want to search for a specific file in a directory which have a "b" letter as the 3rd character in the name of the file. For Example : /abc/efg/ldbjfblkj.sh /abc/efg/erublkd.sh /abc/efg/eibueora.sh /abc/efg/kfvnmnb.sh Since we have 2 files with "b" as a 3rd character in... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

Terminate left side portion of a string

I have a awk file which consists of the follwoing code in file select.awk : /xxx/ { time = gensub(/xxx \*\*\*(.*)/, "\\1", "g") printf("%s\n",time) next } and an input file with the following file file.txt :- xxx ***Wed May 2 18:00:00 CDT 2012 AAA AAAA AAAA xxx... (4 Replies)
Discussion started by: shikshavarma
4 Replies

5. Shell Programming and Scripting

awk search between 2 digits a string

I would like to search between two a string. I thought this would be easy. The is always at the beginning of a line. The code: gawk '/^/{d=$1},/searchstring/,/^(d+1)/' or gawk '/^/,/searchstring/,/^/' did not return the desired result. inputfile.txt 999 some text searchstring some... (6 Replies)
Discussion started by: sdf
6 Replies

6. Shell Programming and Scripting

Insert a special character $ in place of extra spaces

Hi Experts, I have called some.txt with the following content. oracle HYRDSRVIHUB01 pts/0 TESTIHUB 07-JUN-10 CREATE TABLE TESTIHUB PHONE ... (12 Replies)
Discussion started by: naree
12 Replies

7. Shell Programming and Scripting

find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format. ua07app201 (20 Replies)
Discussion started by: honglus
20 Replies

8. Shell Programming and Scripting

Extract digits at end of string

I have a string like xxxxxx44. What's the best way to extract the digits (one or more) in a ksh script? Thanks (6 Replies)
Discussion started by: offirc
6 Replies

9. UNIX for Dummies Questions & Answers

to check if a string has only digits

Hi guys, I am not very experienced in writing ksh scripts and I am trying to write a piece of code that indicates if a given string contains only digits and no alphabet (upper or lower case). If i write it my way it would turn out to have a lot of comparisons.. :eek: Thanks a lot in... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

How to get the most left hand string ??

Hi, I remember once seeing a way to get the left most string in a word. Let's say: a="First.Second.Third" (separated by dot) echo ${a#*.} shows --> Second.Third echo ${a##*.} shows --> Third How do I get the the left most string "First" Or "First.Second" ??? Tried to replace #... (2 Replies)
Discussion started by: jfortes
2 Replies
Login or Register to Ask a Question

Featured Tech Videos