Search and replace string

A

Ajay

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
 
J

Jon Landenburer

Ajay said:
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
 
A

Ajay

Ian said:
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
 
G

gnari

Ajay said:
[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
 
G

Gunnar Hjalmarsson

Ajay said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top