![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
|
|
|||||
|
I suppose it's version/environment specific.
A User’s Guide for GNU Awk Edition 3 June, 2004 Quote:
|
|
||||
|
The
Code:
printf "%'d " This solution Code:
echo 1 12 123 1234 12345 123456 1234567 | awk --re-interval '{print gensub(/([[:digit:]])([[:digit:]]{3})/,"\\1,\\2","g")}'
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)
}
}
}'
|
![]() |
| Bookmarks |
| Tags |
| linux, ubuntu |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|