thousands separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting thousands separator
# 1  
Old 02-12-2008
thousands separator

Hi,
Trying to represent a number with thousands separator in AWK:

Code:
echo 1 12 123 1234 12345 123456 1234567 | awk --re-interval '{print gensub(/([[:digit:]])([[:digit:]]{3})/,"\\1,\\2","g")}' 

  1 12 123 1,234 1,2345 1,23456 1,234567

any idea what is wrong here ?
# 2  
Old 02-12-2008
I would use this (as it seams you have GNU Awk):

Code:
% cat sep.awk
{ printf "%'d ", $1 } END { print "" }
% print 1 12 123 1234 12345 123456 1234567 |gawk -f sep.awk RS=" "
1 12 123 1,234 12,345 123,456 1,234,567

Your locale must support such characters:

Code:
% print 1 12 123 1234 12345 123456 1234567 |LC_ALL=C gawk -f sep.awk RS=" "
1 12 123 1234 12345 123456 1234567
% print 1 12 123 1234 12345 123456 1234567 |LC_ALL=en_US.UTF-8 gawk -f sep.awk RS=" "
1 12 123 1,234 12,345 123,456 1,234,567

# 3  
Old 02-12-2008
I get the following output:
Code:
%'d %'d %'d %'d %'d %'d %'d

I am using redhat release 4
# 4  
Old 02-12-2008
I suppose it's version/environment specific.

A User's Guide for GNU Awk
Edition 3
June, 2004

Quote:
' A single quote or apostrohe character is a POSIX extension to ISO C. It indicates
that the integer part of a floating point value, or the entire part of an
integer decimal value, should have a thousands-separator character in it. This
only works in locales that support such characters. For example:
$ cat thousands.awk # Show source program
a BEGIN { printf "%'d\n", 1234567 }
$ LC_ALL=C gawk -f thousands.awk # Run it in "C" locale
a 1234567
$ LC_ALL=en_US.UTF-8 gawk -f thousands.awk # Run in US English
UTF locale
a 1,234,567
# 5  
Old 02-12-2008
maybe it works for the manual
still it doesn't work for me Smilie
# 6  
Old 02-12-2008
Quote:
Originally Posted by ynixon
maybe it works for the manual
still it doesn't work for me Smilie
Not only for the manual, it works fine on my Ubuntu 7.10 Smilie
# 7  
Old 04-12-2008
Quote:
Originally Posted by ynixon
maybe it works for the manual
still it doesn't work for me Smilie
The
Code:
printf "%'d "

solution did not work for me either. I have GNU AWK 3.1.5. The man doesn't mention apostrophe among printf format options, and I don't have thousands.awk file.

This solution
Code:
echo 1 12 123 1234 12345 123456 1234567 | awk --re-interval '{print gensub(/([[:digit:]])([[:digit:]]{3})/,"\\1,\\2","g")}'

will not work, because only the #,### pattern gets repeated. It becomes more clear when you add a few longer numbers to the list.

I created the following solution:
Code:
#!/bin/sh 
nums=`echo -e " 1\n 12\n 123\n 1234\n 12345\n 123456\n 1234567\n 12345678\n 123456789\n 1234567890\n"`
echo "$nums" | awk --re-interval '{ 
        if (length($1) > 3) 
        {
                a = int(length($1)%3)
                
                if (a == 0)
                {
                        p1 = gensub(/([[:digit:]]{3})/, "\\1,", "g")
                        printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", p1)
                }

                if (a == 1)
                {
                        q1 = gensub(/\<([[:digit:]])/, "\\1,", "g")
                        q2 = gensub(/([[:digit:]]{3})/, "\\1,", "g", q1)
                        printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", q2)
                }
                
                if (a == 2)
                {
                        r1 = gensub(/\<([[:digit:]]{2})/, "\\1,", "g")
                        r2 = gensub(/([[:digit:]]{3})/, "\\1,", "g", r1)
                        printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", r2)
                }
        }
}'

Note! This will not work with non-integers (i don't need it for my script), but it can be extended with some effort!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash-awk to process thousands of files

Hi to all, I have thousand of files in a folder with names with format "FILE-YYYY-MM-DD-HHMM" for what I want to send the following AWK command awk '/Code.*/' FILE-2014* I'd like to separate all files that have the same date to a folder named with the corresponding date. For example, if I... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

2. Shell Programming and Scripting

Search for patterns in thousands of files

Hi All, I want to search for a certain string in thousands of files and these files are distributed over different directories created daily. For that I created a small script in bash but while running it I am getting the below error: /ms.sh: xrealloc: subst.c:5173: cannot allocate... (17 Replies)
Discussion started by: danish0909
17 Replies

3. Shell Programming and Scripting

help to parallelize work on thousands of files

I need to find a smarter way to process about 60,000 files in a single directory. Every night a script runs on each file generating a output on another directory; this used to take 5 hours, but as the data grows it is taking 7 hours. The files are of different sizes, but there are 16 cores... (10 Replies)
Discussion started by: vhope07
10 Replies

4. UNIX for Dummies Questions & Answers

How to truncate thousands of file names

Folder of e-mails in maildir format had been corrupted. Typical file name is 1246281161.6777.m21JH:2,S . The " :2,S prevents " copying to another device. How can I simply remove the last four characters? (2 Replies)
Discussion started by: steve900
2 Replies

5. UNIX for Advanced & Expert Users

Copying Thousands of Tiny or Empty Files?

There is a procedure I do here at work where I have to synchronize file systems. The source file system always has three or four directories of hundreds of thousands of tiny (1k or smaller) or empty files. Whenever my rsync command reaches these directories, I'm waiting for hours for those files... (3 Replies)
Discussion started by: deckard
3 Replies

6. Solaris

script for Gzip thousands of file

Hi experts, I have thousands of file (data file and Gziped file) in same directory like below-- bash-2.05$ pwd /home/mmc bash-2.05$ file PP023149200709270546 TT023149200709270546: gzip compressed data - deflate method bash-2.05$ file PP027443200711242320 TT027443200711242320: ... (10 Replies)
Discussion started by: thepurple
10 Replies

7. Shell Programming and Scripting

Finding a specific pattern from thousands of files ????

Hi All, I want to find a specific pattern from approximately 400000 files on solaris platform. Its very heavy for me to grep that pattern to each file individually. Can anybody suggest me some way to search for specific pattern (alpha numeric) from these forty thousand files. Please note that... (6 Replies)
Discussion started by: aarora_98
6 Replies

8. UNIX for Advanced & Expert Users

Multiple (thousands) of Cron Instances

Hey all, I have a box running SUSE SLES 8 and in the past few months the box will randomly spawn thousands of instances of /USR/SBIN/CRON to the point where the box will lock up entirely. Upwards of 14000 instances! I imagine it's using up all of the available files that can be opened at one... (10 Replies)
Discussion started by: sysera
10 Replies
Login or Register to Ask a Question