The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script for Gzip thousands of file thepurple SUN Solaris 10 01-02-2008 06:39 AM
record separator rochitsharma Shell Programming and Scripting 7 03-04-2006 10:37 AM
Multiple (thousands) of Cron Instances sysera UNIX for Advanced & Expert Users 10 01-17-2006 09:49 AM
Help with unix separator Black mage2021 UNIX for Dummies Questions & Answers 2 01-02-2006 11:49 PM
Separator in Makefile? laila63 Shell Programming and Scripting 2 07-01-2004 11:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-12-2008
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
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 (permalink)  
Old 02-12-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,907
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 (permalink)  
Old 02-12-2008
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
I get the following output:
Code:
%'d %'d %'d %'d %'d %'d %'d
I am using redhat release 4
  #4 (permalink)  
Old 02-12-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,907
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 (permalink)  
Old 02-12-2008
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
maybe it works for the manual
still it doesn't work for me
  #6 (permalink)  
Old 02-12-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,907
Quote:
Originally Posted by ynixon View Post
maybe it works for the manual
still it doesn't work for me
Not only for the manual, it works fine on my Ubuntu 7.10
  #7 (permalink)  
Old 04-12-2008
gllo gllo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Quote:
Originally Posted by ynixon View Post
maybe it works for the manual
still it doesn't work for me
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!
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0