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

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

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

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,833
    Jay B. Harlow [MVP - Outlook]
    Sep 28, 2004
  2. prem_eda
    Replies:
    5
    Views:
    9,181
    Pieter Hulshoff
    Oct 11, 2004
  3. Replies:
    3
    Views:
    13,853
    Boris Stumm
    Feb 9, 2006
  4. Matthew Wilson

    negative numbers and integer division

    Matthew Wilson, Oct 3, 2003, in forum: Python
    Replies:
    7
    Views:
    585
    mensanator
    Oct 3, 2003
  5. Tomi Silander
    Replies:
    3
    Views:
    1,264
    Grant Edwards
    Apr 6, 2005
  6. Frederick Gotham

    Working with negative numbers

    Frederick Gotham, Jun 27, 2006, in forum: C Programming
    Replies:
    39
    Views:
    1,166
  7. Casey

    getopt with negative numbers?

    Casey, Sep 27, 2007, in forum: Python
    Replies:
    24
    Views:
    2,186
    Carl Banks
    Sep 30, 2007
  8. ariel ledesma

    negative numbers are not equal...

    ariel ledesma, Aug 14, 2008, in forum: Python
    Replies:
    8
    Views:
    363
    Terry Reedy
    Aug 15, 2008
Loading...