Search and replace problem

D

Devop

HI. every one.

I wanted to search a string from a file, then replace it, and write to
the file again.

But i have come up with this solution, where the replaced contents will
be written in to the an other file.

But it does not work.

This error is coming :
Use of uninitialized value in substitution (s///)

Is there any other way to solve this problem. Thanks.


#!/usr/bin/perl -w
#use strict;
use warnings;

my $infile = 'first.xml';
my $outfile = 'second.xml';


my $text1 = 'this is it =';
my $text2 = '{and this is a new} OK '';
undef $/;
open IN, $infile or die "couldn't open the file!";
my $intext = <IN>;
close IN;

$intext =s/$text1/$text2/gi;

open OUT, ">$outfile" or die $!;
print OUT $intext;
close OUT;
 
B

Beable van Polasm

Devop said:
This error is coming :
Use of uninitialized value in substitution (s///)

Is there any other way to solve this problem. Thanks.


#!/usr/bin/perl -w
#use strict;

Please don't comment out use strict. It is your friend.
use warnings;
Good!

my $infile = 'first.xml';
my $outfile = 'second.xml';


my $text1 = 'this is it =';
my $text2 = '{and this is a new} OK '';
^^
This is wrong, surely.
undef $/;
open IN, $infile or die "couldn't open the file!";

This should be;
open IN, $infile or die "couldn't open the file: $!";
^^
Tells you what the error was
my $intext = <IN>;
close IN;

$intext =s/$text1/$text2/gi;

Should be:
$intext =~ s/$text1/$text2/gi;
^^
Note typo.
open OUT, ">$outfile" or die $!;
print OUT $intext;
close OUT;

Also, you might like to use one of the XML modules like XML::Simple
to deal with XML files, rather than using regular expressions.

--
 
F

fifo

HI. every one.

I wanted to search a string from a file, then replace it, and write to
the file again.

But i have come up with this solution, where the replaced contents will
be written in to the an other file.

But it does not work.

This error is coming :
Use of uninitialized value in substitution (s///)

Is there any other way to solve this problem. Thanks.


#!/usr/bin/perl -w
#use strict;

Why have you commented this out?
use warnings;

my $infile = 'first.xml';
my $outfile = 'second.xml';


my $text1 = 'this is it =';
my $text2 = '{and this is a new} OK '';

This shouldn't have compiled ----------^
undef $/;
open IN, $infile or die "couldn't open the file!";

Print out $! as well as your message:

open IN, $infile or die "couldn't open the file: $!";
my $intext = <IN>;
close IN;

$intext =s/$text1/$text2/gi;

Here's your error, this should be:

$intext =~ s/$text1/$text2/gi;
 
D

Devop

Thanks a lot. you showed me my mistake


Please don't comment out use strict. It is your friend.

^^
This is wrong, surely.


This should be;
open IN, $infile or die "couldn't open the file: $!";
^^
Tells you what the error was


Should be:
$intext =~ s/$text1/$text2/gi;
^^
Note typo.


Also, you might like to use one of the XML modules like XML::Simple
to deal with XML files, rather than using regular expressions.
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top