Remove trailing zeros from numbers

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Remove trailing zeros from numbers
# 1  
Old 05-24-2017
Remove trailing zeros from numbers

Hi,

I am trying to remove trailing zeros from numbers in a csv file.

Code:
CSV Input : 0.5000,abc,2.00,2.400,285.850,285a.850,205.180800,mno000,a0b0,2.860

Expected Output :
.5,abc,2,2.4,285.85,285a.850,205.1808,mno000,a0b0,2.86

Can you please help.

Thanks.

Last edited by Scrutinizer; 05-25-2017 at 03:36 AM.. Reason: quote tags ====>>>> CODE tags
# 2  
Old 05-24-2017
Code:
awk -F, '
        {
                for( i = 1; i <= NF; i++ )
                {
                        if ( $1 ~ /\./ && $i !~ /[A-Za-z]/ )
                                sub( /0*$/, X, $i )
                }
        }
        1
' OFS=, file.csv

# 3  
Old 05-24-2017
Thanks Yoda.

But it's not giving desired output.

I started trying something with sed like below:

Code:
| rev | sed -r 's/(^|,)0+([1-9])([0-9]*)(.)([0-9]*)(,|$)/\1\2\3\4\5\6/' | rev

But it doesn't work too.

Thanks.
# 4  
Old 05-24-2017
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
# 5  
Old 05-24-2017
Hello manubatham20,

Could you please try following and let me know if this helps you.
Code:
awk -F, '{for(i=1;i<=NF;i++){gsub(/^0+/,"",$i);if($i ~ /\.[0-9]/ && $i !~ /[a-zA-Z]/){gsub(/0+$|\.0+$/,"",$i)};}}1' OFS=,   Input_file

EDIT: Adding a non-one liner solution for same now successfully.
Code:
awk -F, '{
                for(i=1;i<=NF;i++){
                                        gsub(/^0+/,"",$i);
                                        if($i ~ /\.[0-9]/ && $i !~ /[a-zA-Z]/){
                                                                                gsub(/0+$|\.0+$/,"",$i)
                                                                              };
                                  }
         }
         1
        ' OFS=,   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-24-2017 at 02:48 PM.. Reason: Adding a non-one liner solution for same now successfully.
# 6  
Old 05-24-2017
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.

Thanks Ravinder,

Your solution is really helpful, except two cases.

Input:
Code:
000.5000a,0000.0000,a2.500200,0.5000,abc,2.00,2500,2.400,285.850,285a.850,205.1080800,mno000,a0b0,2.860,abc

Output from your awk:
Code:
.5000a,a2.500200,.5,abc,2,2500,2.4,285.85,285a.850,205.10808,mno000,a0b0,2.86,abc

Desired output:
Code:
000.5000a,a2.500200,.5,abc,2,2500,2.4,285.85,285a.850,205.10808,mno000,a0b0,2.86,abc

Note that, solution should only remove zeros from number, not the string values. Also when the value is like 0*.0* (i.e. 000.00000, 00.00, 0000.00, it should be represented as just 0, while it's blank currently)

Thanks for your help.

Last edited by Scrutinizer; 05-25-2017 at 03:39 AM.. Reason: quote tags => code tags
# 7  
Old 05-24-2017
Hello manubatham20,

I think your very first post was not having this detail, could you please try following and let me know if this helps you.
Code:
awk -F, '{for(i=1;i<=NF;i++){if($i ~ /\.[0-9]/ && $i !~ /[a-zA-Z]/){gsub(/0+$|\.0+$|^0+/,"",$i)};}}1' OFS=,   Input_file

EDIT: Adding a non-one liner form of solution successfully too now.
Code:
awk -F, '{
                for(i=1;i<=NF;i++){
                                        if($i ~ /\.[0-9]/ && $i !~ /[a-zA-Z]/){
                                                                                gsub(/0+$|\.0+$|^0+/,"",$i)
                                                                              };
                                  }
         }
         1
        ' OFS=,    Input_file

Thanks,
R. Singh
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