Removing trailing zeros using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing trailing zeros using sed
# 1  
Old 03-23-2010
Removing trailing zeros using sed

Hello All,
I have a csv file with 3 columns. The file which looks like this

Code:
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.
Code:
4785,100,233
2356,10000,456
7865,560000,54
3400,3456,3

How can I achieve this using sed?
# 2  
Old 03-23-2010
Quote:
Originally Posted by grajp002
... The output file should appear as follows.
Code:
4785,100,233
2356,10000,456
7865,560000,54
3400,3456,3

How can I achieve this using sed?
Maybe something like this ?

Code:
$ 
$ 
$ cat -n f9
     1 47850000,100,233
     2 23560000,10000,456
     3 78650000,560000,54
     4 34000000,3456,3
$ 
$ sed 's/^\([0-9][0-9][0-9][0-9]\)[^,]*\(,.*\)$/\1\2/' f9
4785,100,233
2356,10000,456
7865,560000,54
3400,3456,3
$ 
$

tyler_durden
# 3  
Old 03-23-2010
Code:
sed 's/^\(.[^,]*\)0000,\(.*\)/\1,\2/' file

# 4  
Old 03-23-2010
Code:
awk -F "," '{ if ($1~/0000$/) {$1=substr($1,1,length($1)-4)}}1' OFS="," urfile


Last edited by rdcwayx; 03-24-2010 at 12:37 AM..
# 5  
Old 03-23-2010
Code:
sed 's/0000,/,/' file

# 6  
Old 03-23-2010
Quote:
Originally Posted by alister
Code:
sed 's/0000,/,/' file


i think it would remove 0's from each field

it shd be
Code:
sed 's/0000,/,/1' file

and that would still be wrong if the first field doesnot have any zero then it would remove from the subsequent fields

Last edited by daptal; 03-23-2010 at 11:50 PM.. Reason: tags added
# 7  
Old 03-24-2010
Quote:
Originally Posted by daptal
i think it would remove 0's from each field

it shd be
Code:
sed 's/0000,/,/1' file

and that would still be wrong if the first field doesnot have any zero then it would remove from the subsequent fields
The 1 subsitution flag is redundant. By default, SED always subs only the first instance, unless you give it a number or g. There is no difference at all between "sed 's/0000,/,/1'" and "sed 's/0000,/,/'".

The original poster stated that the first column contains 4 trailing zeroes. My sed will remove them. If only some of the rows in the first column contain the trailing zeroes, then the mistake is in the original poster's problem statement for being imprecise. I assume that the problem is stated correctly and choose not to overcomplicate my proposed solution.

Regards,
Alister

---------- Post updated at 11:30 PM ---------- Previous update was at 11:25 PM ----------

However, if it did need to handle a first field that may not contain the four trailing zeroes, the correct sed would be:
Code:
sed 's/^\([^,]*\)0000,/\1,/' file

Smilie
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

Removing Trailing Line

I have been trying to remove empty lines and lines just filled with spaces. I have used the following command which does work. sed -i "/^\s*$/d" Except it leaves one single trailing line at the very end of the file. For the life of me I cant figure out why I cant remove that last trailing... (2 Replies)
Discussion started by: user8282892
2 Replies

3. UNIX for Dummies Questions & Answers

Removing trailing characters

I have been given a shell script that I need to amend. To do the following extract the filename from the flag file by removing the .flag extension. # Local variables # Find if the flag files exists MASK=coda_mil2*.flag # Are there any files? bookmark="40" fileFound=0 ls -1... (3 Replies)
Discussion started by: andymay
3 Replies

4. Shell Programming and Scripting

Removing just the trailing commas :-(

Hi all, I haven't needed to do any shell based editing for nearly 20 years, and no amount of searching around has found me a solution to this very simple problem :-( I have a csv file. Some lines have three commas at the end. This means the invoice hasn't been paid. I'd like to use sed / grep... (4 Replies)
Discussion started by: chardyzulu
4 Replies

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

6. Shell Programming and Scripting

Removing trailing zeroes

So, I can't figure out how to do a previous question with printf, so I'm taking a different approach. Suppose I have a set of numbers: 1200,135.000000,12.30100,3212.3200,1.759403,,1230,101.101010,100.000000 I want to remove all trailing zeroes after the decimal, and, if it ends up orphaned,... (8 Replies)
Discussion started by: treesloth
8 Replies

7. Shell Programming and Scripting

sed: removing any and all trailing digits?

We have a large number of oracle database related scripts that utilize the environment variables $ORACLE_SID and $DBNAME. In a single instance database the $ORACLE_SID is the same as the database name $DBNAME. So we have simply set DBNAME = $ORACLE_SID. However, now that we are clustering with RAC,... (5 Replies)
Discussion started by: Squeakygoose
5 Replies

8. Shell Programming and Scripting

Removing Zeros in front of a number

Hi All, I would like to trim the following input. My condition is as long as there's a zero on the left of the number, remove the zeros. Can anybody help me by using sed or awk ? Eg: 0011 => change to => 11 0333 => change to => 333 4444 => No change => 4444 (13 Replies)
Discussion started by: Raynon
13 Replies

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

10. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies
Login or Register to Ask a Question