Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Search and replace string

Reply
Thread Tools

Search and replace string

 
 
Ajay
Guest
Posts: n/a
 
      02-11-2004
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.3 2.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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-11-2004
Ajay wrote:
> I've to search and replace the value of strings from a file.


http://www.perldoc.com/perl5.8.0/pod...CEMENT-egimosx

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
ajay
Guest
Posts: n/a
 
      02-11-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in message news:<c0d5c5$159kbn$(E-Mail Removed)-berlin.de>...
> Ajay wrote:
> > I've to search and replace the value of strings from a file.

>
> http://www.perldoc.com/perl5.8.0/pod...CEMENT-egimosx


Nice link tx

i expected a clearcut solution
 
Reply With Quote
 
Jon Landenburer
Guest
Posts: n/a
 
      02-11-2004
Ajay <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Ian Cass
Guest
Posts: n/a
 
      02-11-2004
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


 
Reply With Quote
 
Ajay
Guest
Posts: n/a
 
      02-12-2004


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

 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      02-12-2004
"Ajay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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




 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-12-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
simple string search and replace Kun Python 4 03-26-2006 04:15 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM
string-search-and-replace function wanted Stefan Ram C Programming 11 03-04-2004 03:19 PM



Advertisments