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:

 
FR002_15_20
SD475_5_10.5
FG5647_12.25_15.5
BH2463_30.555_32

Any suggestions with awk or sed, or perhaps perl would be greatly appreciated. Thanks in advance,
Theflamingmoe

if there are exactly three decimals then

 sed 's/.000//g' 

will work.

Check this:

echo '171000'|sed 's/.000//g'
17

Hi,

Try this out,

sed 's/\.*00*$//' file

@Ashik409: You have to consider the highlighted.

Cheers,
Ranga :slight_smile:

1 Like

Try this:

sed 's/0*_/_/g;s/0*$//;s/\._/_/g;s/\.$//'

Could probably be done better, but it does the trick

1 Like

Slight refinement to prevent unnecessary replacements:

awk -F_ '{for(i=1;i<=NF;i++){if(sub(/^[0-9]+\.[0-9]+$/,"&",$i)){sub(/0+$/,"",$i);sub(/\.$/,"",$i)}}}1' OFS=_ file

This will trim the zeroes only from the end of the lines.

1 Like

got the question wrong...:confused:

OP's requirement:

Desired output file would be:


Code:
 
FR002_15_20
SD475_5_10.5
FG5647_12.25_15.5
BH2463_30.555_32

already edited in original post...

bit :confused:

Thanks Ranga... i Missed special character... :b:

Thanks very much for the replys :). I originally mucked around with a few pieces of code such as:

sed 's/\(\.[1-9][1-9]\)0/\1/' inputfile

and

awk '{gsub(/\.000/,"",$1); print $1}' inputfile

I couldn't get an overall solution. Couldn't get the sed code to work either, not sure what I was doing wrong. Anyway, will test solutions tomorrow when at work. Thanks again for your input.

Theflamingmoe