Search and replace string

Discussion in 'Perl Misc' started by Ajay, Feb 11, 2004.

  1. Ajay

    Ajay Guest

    Hi All,
    I've to search and replace the value of strings from a file.
    The first string "giccip" can have multiple instances but i have to replace it's
    value from the first instance only.
    In the file "gicc" appear as follows, and it appears in the last of that line:

    giccip=0x0a0600b0

    The second string "STC1NSAP" has only one instance and i have to replace it's
    value. It's on its own line

    STC1NSAP=32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32

    Here is my perl script
    I'm on solaris

    #!/usr/local/bin/perl -w
    ###########################################################
    ## This script changes the giccip address
    ## and STC1NSAP addresses in odd_persist file
    ###########################################################

    #use warning;
    #use strict;

    my $FALSE = 0;
    my $TRUE = 1;
    my $FOUND = $FALSE;
    my $odd11dir = "odd11";
    my $odd11file = "odd11/odd_persist";
    my $slot11ip = "0x0a0600b0";
    my $STC1NSAP12 = "32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32.32";

    ### check if the script is run from the correct directory
    ### check if odd_persist files exist in odd1 & odd12 dirs

    if (! -e $odd11dir)
    {
    die("directory $odd11dir not found \n");
    }
    if ( ! -e $odd11file )
    {
    die("$odd11file file doesn't exist \n");
    }

    ### open odd11/persist file and replace first giccip address with correct value
    ### and replace STC1NSAP Address
    open(ODD11, $odd11file) || die("can't open $odd11file : $!\n");

    @lines = <ODD11>;# Read it into an array
    foreach $line (@lines)
    {
    if ($line =~ /giccip/ && $FOUND == $FALSE )
    {
    #search for first giccip and replace it's value with new value
    ##### how to replace ???
    $FOUND = $TRUE;
    print ($line);
    }
    if ($line =~ /STC1NSAP/)
    {
    # How to replace ??
    print ($line);
    }
    }
    if ( $FOUND == $FALSE )
    {
    die("Can't find giccip");
    }
    close(ODD11);# Close the file
     
    Ajay, Feb 11, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Feb 11, 2004
    #2
    1. Advertising

  3. Ajay

    ajay Guest

    ajay, Feb 11, 2004
    #3
  4. Ajay <> wrote in message news:<>...
    > Hi All,
    > I've to search and replace the value of strings from a file.
    > The first string "giccip" can have multiple instances but i have to replace it's
    > value from the first instance only.
    > In the file "gicc" appear as follows, and it appears in the last of that line:
    >

    Thougt I understood what you were doing until I rwead the code but I
    can tell you if you do a search and replace. The replacement will
    only effect the FIRST item found unless you make it a global replace.

    $A = "AAAABBABABABBABBABBABBABBBA";
    $A =~ s/A/X/; ## SINGLE REPLACE
    print "$A\n";
    $A =~ s/A/Z/g; ## GLOBAL
    print "$A\n";


    !./a.pl
    XAAABBABABABBABBABBABBABBBA
    XZZZBBZBZBZBBZBBZBBZBBZBBBZ
    [Hit return to continue]

    woof
     
    Jon Landenburer, Feb 11, 2004
    #4
  5. Ajay

    Ian Cass Guest

    ajay wrote:

    > i expected a clearcut solution


    I think the guys here will help you to help yourself but won't do your work
    for you.

    --
    Ian Cass
     
    Ian Cass, Feb 11, 2004
    #5
  6. Ajay

    Ajay Guest

    Ian Cass wrote:
    >
    > ajay wrote:
    >
    > > i expected a clearcut solution

    >
    > I think the guys here will help you to help yourself but won't do your work
    > for you.

    yeh that's right
    But i'm stuck that's why i posted this problem

    > --
    > Ian Cass
     
    Ajay, Feb 12, 2004
    #6
  7. Ajay

    gnari Guest

    "Ajay" <> wrote in message
    news:...
    > Hi All,

    [snipped replace problem]

    [snipped program]

    your program (after the replace) would only print the 2 lines in
    question. I thought you wanted the whole file, but with these lines changed.

    the usual way to do that is make a program that reads a file,
    makes the changes, and prints all the lines. then you call it like:

    myprog orig_file > new_file
    or
    someprogram_making_output | myprog > fixed_output

    if you do that you can let perl deal with opening files and looping and
    stuff
    by using commandline switches

    attention: no line break in command line:
    perl -pe '$found=1 if !$found &&
    s/cgipp=(\S+)/cgipp=0x0600b0/;s/STINCSAP=(\S+)/STINCSAP=blablabla/'
    orig_file > new_file

    if you want you can do 'inplace' replace
    perl -pi.bak -e '$found=1 if !$found &&
    s/cgipp=(\S+)/cgipp=0x0600b0/;s/STINCSAP=(\S+)/STINCSAP=blablabla/'
    orig_file

    this will change the original file, but make a backup copy ori_file.bak

    (please test the above before using it on anything critical, as I just typed
    it
    without any testing, and I may have misunderstood your requirements)

    gnari
     
    gnari, Feb 12, 2004
    #7
  8. Ajay wrote:
    > Ian Cass wrote:
    >> ajay wrote:
    >>> i expected a clearcut solution

    >>
    >> I think the guys here will help you to help yourself but won't do
    >> your work for you.

    >
    > yeh that's right
    > But i'm stuck that's why i posted this problem


    Then post your best attempt you made by help of the documentation and
    somebody may be willing to help you correct it.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 12, 2004
    #8
    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. Stefan Ram

    string-search-and-replace function wanted

    Stefan Ram, Mar 4, 2004, in forum: C Programming
    Replies:
    11
    Views:
    827
    anony*mouse
    Mar 4, 2004
  2. Alun
    Replies:
    3
    Views:
    4,515
    Masudur
    Feb 18, 2008
  3. Abby Lee
    Replies:
    5
    Views:
    415
    Abby Lee
    Aug 2, 2004
  4. Prasad S
    Replies:
    2
    Views:
    234
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    307
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page