Need to change format of number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to change format of number
# 8  
Old 09-13-2009
In that case, the compulsive consumer that I am agrees.Smilie
# 9  
Old 09-14-2009
maybe you can try below perl

Code:
open FH,"<a.txt";
while(<FH>){
  chomp;
  my @tmp=split(",",$_);
  map {s/(?=(?:[0-9]{3})+(?:\.[0-9]*)?$)/,/g;} @tmp;
  print join ":", @tmp;
  print "\n";
}
close FH;

# 10  
Old 09-15-2009
Thanks for your replies

Tried with all the options provided,
Sorry for the typo in my question , but trying to generate a generic thousand format for all the numbers provided.
Also was hit with a development, to include -ve numbers as well.The function comma works good for positive values, but not able to get around the code to using the function for -ve numbers.

If i try to use the format option,
comma.sh >
Code:
LANG=en_US.UTF-8 nawk -F, -v OFS=":" '{
    for(i=1;i<=NF;i++) {
        l=length($i)
        p=index($i,".")
        d=p?l-p:0;
        $i=sprintf("%'"'"'*.*f",l,d,$i)
    }
    print
}' file.csv


$cat file.csv
-1979181218.70,-4016811628.77,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40,-4177131974.40
1573820407.52,3110040327.95,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01,3133242365.01
-405360811.18,-906771300.82,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39,-1043889609.39


$./comma2.sh
-1979181218.70:-4016811628.77:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40:-4177131974.40
1573820407.52:3110040327.95:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01:3133242365.01
-405360811.18:-906771300.82:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39:-1043889609.39

Not able to find any change in the format for the thousand separator, either for +ve or negative numbers.

Code:
locale -a
POSIX
common
en_US.UTF-8
C
iso_8859_1
en_AU
en_AU.ISO8859-1
en_NZ
en_NZ.ISO8859-1

Smilie. Think this is going to take some time.

---------- Post updated at 01:02 PM ---------- Previous update was at 12:43 PM ----------

A Simple logic hit me and it worked, Smilie
This will work for negative numbers as well.

Code:
function commas(n) {
 if ( n > -1000 && n < 1000 ) return n

 if(n > 1000 || n == 1000)
 {
     gsub(",","",n)
     point = index(n,".") - 1
     if (point < 0) point = length(n)
        while (point > 3) 
        {
             point -= 3
         n = substr(n,1,point) "," substr(n,point + 1)
             }
}

if( n < -1000 || n == -1000)
 {
        gsub(",","",n)
n=substr(n,2,length(n));
         point = index(n,".") - 1
         if (point < 0) point = length(n) ;
                while (point > 3)
                {
                 point -= 3
                 n = substr(n,1,point) "," substr(n,point + 1)
                 }
n= "-" n
 }
 return n
}

Thanks for your response Smilie
# 11  
Old 09-15-2009
It seems to work fine with the en_US.UTF-8 locale you also have on you box. Don't forget to temporarily force the locale to that en_US value at the run time of your script.

For a variable number of decimal (i.e. no decimal at all when not in input file):
Code:
$ LANG=en_US.UTF-8 awk -F, -v OFS=":" '{for(i=1;i<=NF;i++)$i=sprintf("%\047*.*f",length($i),index($i,".")?length($i)-index($i,"."):0,$i)}1' file
-1,979,181,218.70:-4,016,811,628.77:-1,979,181,218.70:-4,016,811,628.77:-4,177,131,974.40:-4,177,131,974.40:-4,177,131,974.40:-4,177,131,974.40
1,573,820,407.52:3,110,040,327.95:3,133,242,365.01:3,133,242,365.01:3,133,242,365.01:3,133,242,365.01
-405,360,811.18:-906,771,300.82:-1,043,889,609.39:-1,043,889,609.39:-1,043,889,609.39:-1,043,889,609.39

For a fixed nimber of decimal even when no decimal provided in input file:
Code:
$ LANG=en_US.UTF-8 awk -F, -v OFS=":" '{for(i=1;i<=NF;i++)$i=sprintf("%\047.2f",$i)}1' file
-1,979,181,218.70:-4,016,811,628.77-1,979,181,218.70:-4,016,811,628.77:-4,177,131,974.40:-4,177,131,974.40:-4,177,131,974.40:-4,177,131,974.40
1,573,820,407.52:3,110,040,327.95:3,133,242,365.01:3,133,242,365.01:3,133,242,365.01:3,133,242,365.01
-405,360,811.18:-906,771,300.82:-1,043,889,609.39:-1,043,889,609.39:-1,043,889,609.39:-1,043,889,609.39

If you don't like changing all the locale, you can restrict the change to LC_NUMERIC to the same effect.

Last edited by ripat; 09-15-2009 at 05:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change name format

I am trying to use either awk or sed to make names like J. A. Smith and J.A. Martin Smith become JA Smith and JA Martin SmithThe code should concatenate abbreviated letters that have a dot after them. I have only been able to do this: echo $name | sed 's/\.\ //g'But that concatenates the... (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

Format change

I wish to convert the following in shell. Help me SED or AWK ID= 12345,23456,67859 , 90225 , 67583 I can extract the right hand side of "=" using cut command "cut -d "=" -f2 after extracting that right side i would need like this '12345','23456','67859','90225','67583' 1 ) the... (2 Replies)
Discussion started by: ilugopal
2 Replies

3. Shell Programming and Scripting

Change Date Format

Hi Guys, I had a scenario like this.. It seems very silly...dont think it as a home work question.....:) i tried it many ways but i didn't achieve this... start_date=May122011 here i want to change the start_date in to 20110512 start_date=20110512 tell me how can we achive... (5 Replies)
Discussion started by: apple2685
5 Replies

4. Shell Programming and Scripting

Date Format Change

Hi, Please can I have some command to get yesterday in YYMMDD format? This will be used in ksh Thanks, (2 Replies)
Discussion started by: smalya
2 Replies

5. Shell Programming and Scripting

format change

I have a text file sample.txt which contains some details like Order 9001 Item Code 34 Quantity 4 Entry Date 2009-04-23 Ordered by Ram Order 9002 Item Code 34 Quantity 3 (1 Reply)
Discussion started by: lazydev
1 Replies

6. Shell Programming and Scripting

Change date format

Hi guys, I have a text file with lots of lines like this: MCOGT23R27815 27/07/07 27/05/09 SO733AM0235 30/11/07 30/11/10 NL123403N 04/03/08 04/03/11 0747AM7474 04/04/08 04/04/11 I want to change each line so the date format looks like this: MCOGT23R27815 07/07/27 09/05/27 ... (7 Replies)
Discussion started by: Tornado
7 Replies

7. Shell Programming and Scripting

Change of date format

I want to chnage the date format from the file format like below to WT;T15D;0000007208;;20080401;3;0;0;3;;B;ZZZZZZ; WT;T25D;0000007208;;20080401;6;0;0;6;;B;ZZZZZZ; WT;T5D;0000007208;;20080401;123;0;0;123;;B;ZZZZZZ; to WT;T15D;0000007208;;04/01/200804;3;0;0;3;;B;ZZZZZZ;... (2 Replies)
Discussion started by: svenkatareddy
2 Replies

8. UNIX for Advanced & Expert Users

Change date format

I know the command date +"%Y%m%d" can change today's date to digit format as below . $date +"%Y%m%d" 20071217 it works fine . now I want to do it back . If I have a file like below, (in the file , there are three lines, and each line have ; sign , after the ; sign is the date ) , I... (4 Replies)
Discussion started by: ust
4 Replies

9. UNIX for Dummies Questions & Answers

How to change it to the date format

Hi, I want to know how to change this string to date format 20061102122042 to 02-11-2006 12:20:42 or 02-Nov-2006 12:20:42 Please let me know at the earliest.Thanks in advance. Regards, Preetham R. (3 Replies)
Discussion started by: preethgideon
3 Replies

10. Shell Programming and Scripting

change the empty function from the old format to the new format

I have about 300 files which has the function getDBBackend(). How to write a program to change the empty function from the old format to the new format? Old empty function format are either: function getDBBackend() { // Not available } // getDBBackend or: function... (0 Replies)
Discussion started by: powah
0 Replies
Login or Register to Ask a Question