Removing trailing zeros using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing trailing zeros using sed
# 8  
Old 03-24-2010
or maybe :
sed 's/\(^[0-9][0-9]*\)0000,/\1,/g' file
# 9  
Old 03-24-2010
Quote:
Originally Posted by ygemici
or maybe :
sed 's/\(^[0-9][0-9]*\)0000,/\1,/g' file
The 'g' flag has no effect on this substitution command. Since the pattern is anchored to the beginning of the line, it cannot match in more than one place (if it could match multiple times, then the global flag would be an error since this problem only requires substituting the first occurrence).

Regards,
Alister
# 10  
Old 03-24-2010
try this one

Code:
sed 's/^\([^,|0]*\)00*\(.*\)/\1\2/' filename

# 11  
Old 03-24-2010
Try this :
Code:
rev infile | awk -F ',' '{ print $1",",$2"," substr($3,5)}' | rev


Last edited by Franklin52; 03-24-2010 at 02:05 PM.. Reason: Please use code tags.
# 12  
Old 03-24-2010
Quote:
Originally Posted by asalman.qazi
try this one

Code:
sed 's/^\([^,|0]*\)00*\(.*\)/\1\2/' filename

That assumes that there are no zeroes in the first column prior to the trailing 4. If there is one, the pattern will match a leading or embedded zero, leaving the trailing four intact. Also, if that other zero immediately precedes the trailing four, that command will remove 5 zeroes (or more).
Code:
$ cat data
47850000,100,233
20560000,10000,456
78600000,560000,54
34000000,3456,3

$ sed 's/^\([^,|0]*\)00*\(.*\)/\1\2/' data
4785,100,233
2560000,10000,456
786,560000,54
34,3456,3

I strongly suggest that the above code not be used unless the only zeroes in first field are the trailing four.

Regards,
Alister

Last edited by alister; 03-24-2010 at 02:46 PM..
# 13  
Old 03-24-2010
MySQL

Quote:
Originally Posted by alister
The 'g' flag has no effect on this substitution command. Since the pattern is anchored to the beginning of the line, it cannot match in more than one place (if it could match multiple times, then the global flag would be an error since this problem only requires substituting the first occurrence).

Regards,
Alister
I know this already.But there is not a problem in this command.
global flag on this example is not necessary but i use because of
not make a difference.

For example i add one word that is matched (1212000000)

Code:
[root@rhnserver ~]# sed 's/\(^[0-9][0-9]*\)0000,/\1,/g' data
4785,100,233,1212000000
2056,10000,456
7860,560000,54
3400,3456,3
[root@rhnserver ~]# sed 's/\(^[0-9][0-9]*\)0000,/\1,/' data
4785,100,233,1212000000
2056,10000,456
7860,560000,54
3400,3456,3

This shown not return any error by g flag and the result is same.
g flag returns results every word on the line instead of the first in the same line.Our pattern with start ^ .. Consequently matched result exist only one for every lines that g flag is or is not..


Thanks for your ideas

Last edited by Scott; 03-24-2010 at 06:27 PM.. Reason: Please use code tags
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