Remove trailing zeros


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove trailing zeros
# 1  
Old 09-05-2012
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:

Code:
 
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:

Code:
 
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
# 2  
Old 09-05-2012
if there are exactly three decimals then
Code:
 sed 's/.000//g'

will work.
# 3  
Old 09-05-2012
Quote:
Originally Posted by Ashik409
if there are exactly three decimals then
Code:
 sed 's/.000//g'

will work.
Check this:
Code:
echo '171000'|sed 's/.000//g'
17

# 4  
Old 09-05-2012
Hi,

Try this out,

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

Quote:
Originally Posted by Ashik409
if there are exactly three decimals then
Code:
 sed 's/.000$//g' - Here there is no need for g modifier

will work.
@Ashik409: You have to consider the highlighted.

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 09-05-2012
Try this:

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

Could probably be done better, but it does the trick
This User Gave Thanks to Subbeh For This Post:
# 6  
Old 09-05-2012
Slight refinement to prevent unnecessary replacements:
Code:
awk -F_ '{for(i=1;i<=NF;i++){if(sub(/^[0-9]+\.[0-9]+$/,"&",$i)){sub(/0+$/,"",$i);sub(/\.$/,"",$i)}}}1' OFS=_ file

Quote:
Originally Posted by rangarasan
Hi,

Try this out,

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

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

Last edited by elixir_sinari; 09-05-2012 at 07:09 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 09-05-2012
got the question wrong...Smilie

Last edited by pamu; 09-05-2012 at 07:04 AM.. Reason: corrected..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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. (11 Replies)
Discussion started by: manubatham20
11 Replies

2. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

3. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

4. Shell Programming and Scripting

Remove trailing 0 from the field

Hi Freinds, I have file1.txt as below file1.txt 1521894~~-0.400~201207 1521794~~-0.486~201207 152494~~-0.490~201207 152154894~~-0.490~201207 1521894354~~-0.489~201207 expected output : 1521894~~-0.4~201207 1521794~~-0.486~201207 152494~~-0.49~201207... (9 Replies)
Discussion started by: i150371485
9 Replies

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

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

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

8. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 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