Remove trailing zeros from numbers

Hi,

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

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.

awk -F, '
        {
                for( i = 1; i <= NF; i++ )
                {
                        if ( $1 ~ /\./ && $i !~ /[A-Za-z]/ )
                                sub( /0*$/, X, $i )
                }
        }
        1
' OFS=, file.csv

Thanks Yoda.

But it's not giving desired output.

I started trying something with sed like below:

| 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.

Moderator comments were removed during original forum migration.

Hello manubatham20,

Could you please try following and let me know if this helps you.

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.

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

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:

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:

.5000a,a2.500200,.5,abc,2,2500,2.4,285.85,285a.850,205.10808,mno000,a0b0,2.86,abc

Desired output:

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.

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.

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.

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

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.

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.

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

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...

Thanks Rudic.

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

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.

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

Try

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