Replace negative numbers with 0 in a file

Discussion in 'Perl' started by Mayank, Nov 28, 2003.

  1. Mayank

    Mayank Guest

    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
     
    Mayank, Nov 28, 2003
    #1
    1. Advertising

  2. Mayank

    Mike Cross Guest

    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
     
    Mike Cross, Nov 28, 2003
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Nov 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Negative Numbers?

    Charles A. Lackman, Sep 28, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,612
    Jay B. Harlow [MVP - Outlook]
    Sep 28, 2004
  2. prem_eda
    Replies:
    5
    Views:
    7,919
    Pieter Hulshoff
    Oct 11, 2004
  3. Replies:
    3
    Views:
    12,635
    Boris Stumm
    Feb 9, 2006
  4. Matthew Wilson

    negative numbers and integer division

    Matthew Wilson, Oct 3, 2003, in forum: Python
    Replies:
    7
    Views:
    383
    mensanator
    Oct 3, 2003
  5. Tomi Silander
    Replies:
    3
    Views:
    931
    Grant Edwards
    Apr 6, 2005
Loading...

Share This Page