By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Replace negative numbers with 0 in a file

P: n/a
X-No-Archive: yes

Hi,

I have to replace all the negative numbers with 0 in a very large
file. How can I do it in Perl/Awk. Negative numbers are of the format
(-0.02:0.01:-1.00) ( 0.11:0.01:-0.01)

I need to change them to

(0:0.01:0) (0.11:0.01:0)

Positive numbers should remain untouched.

Thanks in advance.

Regards
Mayank
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

I think this SED script will do what you want

sed 's/-[^:)]*/0/g'

Mike
Mayank wrote:
X-No-Archive: yes
Hi,

I have to replace all the negative numbers with 0 in a very large
file. How can I do it in Perl/Awk. Negative numbers are of the format
(-0.02:0.01:-1.00) ( 0.11:0.01:-0.01)

I need to change them to

(0:0.01:0) (0.11:0.01:0)

Positive numbers should remain untouched.

Thanks in advance.

Regards
Mayank


Jul 19 '05 #2

P: n/a
Mike Cross wrote:
Mayank wrote:
I have to replace all the negative numbers with 0 in a very large
file. How can I do it in Perl/Awk. Negative numbers are of the
format
(-0.02:0.01:-1.00) ( 0.11:0.01:-0.01)

I need to change them to

(0:0.01:0) (0.11:0.01:0)


I think this SED script will do what you want

sed 's/-[^:)]*/0/g'


If there is other info in the file, besides such parentheses with
numbers, I'd rather do (in Perl):

s/-\d+(?:\.\d+)?/0/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.