simple question reag search and replace

Discussion in 'Perl Misc' started by sivga, Nov 7, 2007.

  1. sivga

    sivga Guest

    Hi all i need to search for a particular string and replace it in a
    file

    for eg :input file has

    stock 3 99999 6 13456000 -market 1 100 5600 shift 2 rshit 36
    stock 300 99999 6 13456000 -market 2 300 5700 shift 2 rshit 312
    stock 31234 99999 6 13456000 -market 3 400 5700 shift 2 rshit 3675
    stock 456 99999 6 13456000 -market 4 200 5800 shift 2 rshit 30

    ......

    99999 in the above lines needs to be replaced with 77777 so the o/p
    after replace will be

    stock 3 77777 6 13456000 -market 1 100 5600 shift 2 rshit 36
    stock 300 77777 6 13456000 -market 2 300 5700 shift 2 rshit 312

    How do i do this ?

    This does not seem to work ..

    while ($line = <AG>) {

    if ($line =~ /stock[ ]+([99999])*/ ) {
    print " The stock is $line";
    $line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;

    }
    }


    Thanks for the help
     
    sivga, Nov 7, 2007
    #1
    1. Advertising

  2. sivga wrote:
    > while ($line = <AG>) {
    >
    > if ($line =~ /stock[ ]+([99999])*/ ) {


    if ( $line =~ /stock.+\b99999\b/ ) {

    > print " The stock is $line";
    > $line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;


    $line =~ s/\b99999\b/77777/;

    Where did you get the idea of using square brackets that way?

    Have you seen the extensive Perl documentation on regular expressions?

    perldoc perlrequick
    perldoc perlreref
    perldoc perlretut
    perldoc perlre

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 7, 2007
    #2
    1. Advertising

  3. sivga wrote:
    >
    > Hi all i need to search for a particular string and replace it in a
    > file
    >
    > for eg :input file has
    >
    > stock 3 99999 6 13456000 -market 1 100 5600 shift 2 rshit 36
    > stock 300 99999 6 13456000 -market 2 300 5700 shift 2 rshit 312
    > stock 31234 99999 6 13456000 -market 3 400 5700 shift 2 rshit 3675
    > stock 456 99999 6 13456000 -market 4 200 5800 shift 2 rshit 30
    >
    > .....
    >
    > 99999 in the above lines needs to be replaced with 77777 so the o/p
    > after replace will be
    >
    > stock 3 77777 6 13456000 -market 1 100 5600 shift 2 rshit 36
    > stock 300 77777 6 13456000 -market 2 300 5700 shift 2 rshit 312
    >
    > How do i do this ?
    >
    > This does not seem to work ..
    >
    > while ($line = <AG>) {
    >
    > if ($line =~ /stock[ ]+([99999])*/ ) {


    Anything between [ and ] is a character class and any duplicate
    characters are discarded so [99999] and [9] and [9999999999999999] all
    do exactly the same thing. Your pattern says that there are only spaces
    between 'stock' and '99999' but your data says that there is a number
    between them as well.


    > print " The stock is $line";
    > $line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;


    The right hand side of the substitution is just a string *not* a regular
    expression.


    > }
    > }




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 7, 2007
    #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. Kun
    Replies:
    4
    Views:
    314
    Dennis Lee Bieber
    Mar 26, 2006
  2. Gontrand Trudau
    Replies:
    0
    Views:
    286
    Gontrand Trudau
    Jan 13, 2010
  3. Abby Lee
    Replies:
    5
    Views:
    420
    Abby Lee
    Aug 2, 2004
  4. jac
    Replies:
    7
    Views:
    135
    David Squire
    May 24, 2006
  5. laredotornado

    Search and replace question

    laredotornado, Jul 29, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    193
    John W. Krahn
    Jul 30, 2011
Loading...

Share This Page