Remove trailing zeros from numbers

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Remove trailing zeros from numbers
# 8  
Old 05-24-2017
Quote:
Originally Posted by manubatham20
Hi Corona688,

Posting standard comments without knowledge of the context, and without looking in the problem may be your job. When I said, "not desired output" I specified the desired output in my first post.
You did not explain how it didn't work. You just said "did not work". We can throw solutions at 'did not work' all day and come out none the wiser. We need to know more - what actually happened, just just the fact that it didn't work.
# 9  
Old 05-24-2017
Hi Ravinder,

Thats great help. Thanks.

Corona688,

If you have given a few more seconds, and tried the solution given, you may have realized.
I always appreciate promptly help I get from this forum.

Keep it up.

Many thanks,
Manu.
# 10  
Old 05-24-2017
I have to second Corona688 saying that it's far easier for the people in here to immediately see the deviation from your desired output (which you produced anyhow when testing) than to download the input and the proposal, run the latter, and then compare (in the best cases) or imagine a failure.

Howsoever, why not
Code:
awk -F, '
        {for (i=1;i<=NF;i++)    $i = ($i == $i+0)?$i+0:$i
        }
1
' OFS=, CONVFMT="%.8g"  file
0.5,abc,2,2.4,285.85,285a.850,205.1808,mno000,a0b0,2.86

You can't make it drop the lonely 0 in front of the dot, unless you go extra lengths...
# 11  
Old 05-24-2017
Thanks Rudic.

Idea seems nice, it's working fine for the given test case.

Code:
echo 'abc,0.0100' | awk -F, '{for (i=1;i<=NF;i++) {$i = ($i == $i+0)?$i+0:$i}}1' OFS=,

but when I am trying it with quoted strings it's not working.

Code:
echo '"abc","0.0100"' | awk -F, '{for (i=1;i<=NF;i++) {$i=substr($i,2,length($i)-2);$i = ($i == $i+0)?$i+0:$i}}1' OFS=,

Am I doing something wrong?

Thanks,
Manu


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 05-25-2017 at 02:57 AM.. Reason: Changed QUOTE to CODE tags.
# 12  
Old 05-25-2017
Try
Code:
echo '"abc","0.0100"' | awk -F, '{for (i=1;i<=NF;i++) {$i=substr($i,2,length($i)-2); $0=$0; $i = ($i == $i+0)?$i+0:$i}}1' OFS=, 
abc,0.01

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help - Command to Subtract two numbers without losing prefix zeros

Hello, I have a variable LOGNUM with values 0000095, When i subtract the variable by 1, Its losing its leading zeros. Can you please help me here ? LOGNUM=0000095 $OLDLOG=`echo "${LOGNUM}-1"|bc` $ echo $OLDLOG 94 Am expecting output as 0000094 Appreciate your help! Thanks,... (11 Replies)
Discussion started by: prince1987
11 Replies

2. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

3. Shell Programming and Scripting

Remove trailing zeros

Hi I have a simple request but can't find the answer. I want to remove trailing zeros, and in some cases the fullstops, from the input data. Example of input file: FR002_15.000_20.000 SD475_5.000_10.500 FG5647_12.250_15.500 BH2463_30.555_32.000 Desired output file would be: ... (10 Replies)
Discussion started by: theflamingmoe
10 Replies

4. Shell Programming and Scripting

Fill missing numbers in second column with zeros

Hi All, I have 100 files with names like this: 1.dat, 2.dat, 3.dat until 100.dat. My dat files look like this: 42323 0 438939 1 434 0 0.9383 3434 120.23 3 234 As you can see in the second column, some numbers are missing. I want to fill those missing places with 0's in all... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

appending zeros to numbers using awk

hi i want to append zeros to a given number ( varying digits). the total length of the output should be 10 digits. For example: 1)input is var=347 output should be NewVar=0000000347 2) input is var=123456 output should be NewVar=0000123456 i am able to acheive this using typeset... (1 Reply)
Discussion started by: somi2yoga
1 Replies

6. Shell Programming and Scripting

Removing trailing zeros using sed

Hello All, I have a csv file with 3 columns. The file which looks like this 47850000,100,233 23560000,10000,456 78650000,560000,54 34000000,3456,3 The first column has 4 trailing zeros. I have to remove 4 trailing zeroes from 1st field. The output file should appear as follows. ... (12 Replies)
Discussion started by: grajp002
12 Replies

7. Shell Programming and Scripting

How to delete trailing zeros from a variable

Hi All I want to delete trailing zeros from varible. ex: if variable value is 1234.567000 result as 1234.567 if variable has 1234.0000 result as 1234 if variable as abcd.fgh result as abcd.fgh Can somone give me a solution using awk? (16 Replies)
Discussion started by: Chandu2u
16 Replies

8. Shell Programming and Scripting

Remove trailing G

Hello, I am trying to write a script that will calculate the amount of data remaining in a storage volume. I'm running Tru64 Unix version 5.1B patch kit 6. The script is being run against an AdvFS domain. I am programming in Korn Shell version M-11/16/88f. The basic idea is that I want to run df... (3 Replies)
Discussion started by: Heathe_Kyle
3 Replies

9. Shell Programming and Scripting

Remove O and preceeding zeros in a string

Hi all, Can anybody help me out to write a program in perl to remove O and preceeding zeros. for eg input is O0000123089 - output 123089 Thanks Mahalakshmi.A (10 Replies)
Discussion started by: mahalakshmi
10 Replies

10. Shell Programming and Scripting

remove precursing zeros

Hello all, I have a list of reports for stores that are numbered like: s001845,s000022,s198490,s020048,s002385 however the users are displaying the reports as: 1845,22,198490,20048,2385 It isn't real critical but I would like to associate them so they are the same. And since the users are... (2 Replies)
Discussion started by: gozer13
2 Replies
Login or Register to Ask a Question