Re: help on replacing text in a file

2007-11-01 Thread John W . Krahn
On Thursday 01 November 2007 11:25, boxingcat wrote:
> / Sorry if it is a repost. didn't see the first one in the group.
>
>
> I  would like to replace string 1 with string 2 in file3.dat, here is
> what I did:
>
> #!/usr/bin/perl

use warnings;
use strict;


> @inputfile= ("string1");  # need to have an arrary later
> @outputfile=("string2");
> @attfile=("file3.dat");
>
> $mytempfile=$inputfile[0];
> $youtempfile=$outputfile[0];
> $hisfile=$attfile[0];
>
> perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;

You are trying to run an external program in perl (this is not a shell 
script.)

perldoc -f system
perldoc -f qx
perldoc -f open


However you can do what you want directly in perl:

{   local ( $^I, @ARGV ) = ( '', $attfile[ 0 ] );
while ( <> ) {
s/$inputfile[0]/$outputfile[0]/;
print;
}
}




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




help on replacing text in a file

2007-11-01 Thread boxingcat
/ Sorry if it is a repost. didn't see the first one in the group.


I  would like to replace string 1 with string 2 in file3.dat, here is
what I
did:

#!/usr/bin/perl
@inputfile= ("string1");  # need to have an arrary later
@outputfile=("string2");
@attfile=("file3.dat");

$mytempfile=$inputfile[0];
$youtempfile=$outputfile[0];
$hisfile=$attfile[0];

perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;

The program reports an error:

Scalar found where operator expected at test line 10 near 's/
$mytempfile/$
youtempfile/' $hisfile
missing operator before $hisfile?)
Syntax error at test line 10, near "pi -e"

I am new to perl, please help!!!

thanks.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




help on replacing text in a file

2007-11-01 Thread boxingcat
I  would like to replace string 1 with string 2 in file3.dat, here is
what I
did:

#!/usr/bin/perl
@inputfile= ("string1");  # need to have an arrary later
@outputfile=("string2");
@attfile=("file3.dat");

$mytempfile=$inputfile[0];
$youtempfile=$outputfile[0];
$hisfile=$attfile[0];

perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;

The program reports an error:

Scalar found where operator expected at test line 10 near 's/
$mytempfile/$
youtempfile/' $hisfile
missing operator before $hisfile?)
Syntax error at test line 10, near "pi -e"

I am new to perl, please help!!!

thanks.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Newbie having problems replacing text

2004-12-03 Thread Jonathan Paton
Dear Pam,

Obviously, $title is undef.  This is a great example for warnings &
strict, as otherwise this would just silently not do what you want.

Your task is to move the text within the  tags to the 
tags.  You are processing the file, line by line.  When can you expect
to know the page title?  Not until after you find the  tags!

Jonathan Paton

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Newbie having problems replacing text

2004-12-03 Thread Pam Derks
Hi all,

I'm trying to read in a bunch of files and  insert the contents of the 
tag into the  tag, writing the output to a temp file.

Am getting this error:
Use of uninitialized value at title.pl line 28,  chunk 14.

Which is this line:
 $_ =~ s#$pattern2#$title#g;

I'm not sure why I'm getting this error.

Thanks in advance,
Pam


Sample  file



http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en"> 








  







Beginning Acting I

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

my($file, @files, $title);
@ARGV = glob('*.html');
@files = @ARGV;


foreach $file(@files) {
#open files
print("OPEN: $file\n");
open(IN, "$file") or die ("can't open in file: $!");

my $outfile = "$file.tmp";
open(OUT,">$outfile") or die ("can't open outfile: $!"); 
while(){

my $pattern1 = "\\(.*\)<\/h1>";
my $pattern2 = "<\/title>";

if ($_ =~ /$pattern1/){
$title = $1;
print("$_");
#print OUT ("$_");
}

elsif ($_ =~ /$pattern2/){
$_ =~ s#$pattern2#$title#g;
print("$_\n");
#print OUT ("$_");
}
else{
print("$_");
#print OUT ("$_");
}


} #end while file contents
close IN;
close OUT;

} #end foreach





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Searching and replacing text

2004-08-02 Thread Zeus Odin
Could you post the code you have written thus far? It would be a great help.

-ZO

"Chris Richards" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
Hi Everyone,

This is my first post to the list Please be gentle :)

Just so we are all clear... I am completely lost right from the start
with this one. It is far beyond my current Perl skills and I need some
serious help :)

I want to create a script that searches file_b for the first entry in
file_a (router1 in the below example) and once it finds it I would like
it to change the next occurrence of "Parent Submap: Any string" to
"Parent Submap: second entry in file_a (site1 in the below example)"

It would also need to be case-insensitive.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Searching and replacing text

2004-08-02 Thread Chris Richards
Fair enough.

-Chris

-Original Message-
From: Gunnar Hjalmarsson [mailto:[EMAIL PROTECTED] 
Sent: Monday, 2 August 2004 10:08 PM
To: [EMAIL PROTECTED]
Subject: Re: Searching and replacing text

Chris Richards wrote:
> This is my first post to the list Please be gentle :)

Sure.

> Just so we are all clear... I am completely lost right from the start 
> with this one. It is far beyond my current Perl skills

Oh?

> and I need some serious help :)

Then, this is the most serious (and gentle) help I can think of:

 http://learn.perl.org/



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

--
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/>
<http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Searching and replacing text

2004-08-02 Thread Gunnar Hjalmarsson
Chris Richards wrote:
This is my first post to the list Please be gentle :)
Sure.
Just so we are all clear... I am completely lost right from the
start with this one. It is far beyond my current Perl skills
Oh?
and I need some serious help :)
Then, this is the most serious (and gentle) help I can think of:
http://learn.perl.org/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Searching and replacing text

2004-08-02 Thread Chris Richards
Hi Everyone,

This is my first post to the list Please be gentle :)

Just so we are all clear... I am completely lost right from the start
with this one. It is far beyond my current Perl skills and I need some
serious help :)

I want to create a script that searches file_b for the first entry in
file_a (router1 in the below example) and once it finds it I would like
it to change the next occurrence of "Parent Submap: Any string" to
"Parent Submap: second entry in file_a (site1 in the below example)"

It would also need to be case-insensitive. 

Can anyone give me a huge push in the right direction? 

Thank you. 

-Chris

-
External files: 
file_a - list of routers and their location
Eg: 
router1,site1

file_b - output from an openview map export, contains 1000's of entries
like the below example. 
Eg: 
SYMBOL: ROUTER1
Object Type: 4
Parent Submap: IP Internet
Symbol Type: Cisco Router:cisco-Rtr1720Family
Label: ROUTER1
Symbol Position: 1:11764x11691+7126+653
Hidden Value: FALSE
Submap Exists: TRUE
Background Graphics: 
Layout Style: 2
Layout On: TRUE
Submap Overlay: TRUE
Show Connection Labels: FALSE
END SYMBOL

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Replacing text

2003-12-06 Thread John W. Krahn
Dan Anderson wrote:
> 
> I have a  script that reads text from a  file and inserts text
> into different  places depending on  what it needs  to do.  But  I use
> split to replace the text, i.e.:
> 
> ($first_part, $second_part) = split "#INSERT#TEXT#HERE#", $document, 2;
> print FILEHANDLE $firstpart, $text_to_insert, $secondpart;
> 
> Is  there a  replace function  in perl  that would  let  me do
> something like  replace "#INSERT#TEXT#HERE", $text_to_insert;?   I was
> going to  write my own  method but was  curious if perl  had something
> faster?

You could use the substitution operator:

$document =~ s/#INSERT#TEXT#HERE#/$text_to_insert/;


Or you could use a combination of substr(), index() and length():

my $text_to_find = '#INSERT#TEXT#HERE#';
substr $document, index( $document, $text_to_find ), length $text_to_find, 
$text_to_insert;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Replacing text

2003-12-05 Thread Tom Kinzer
Good news, Dan.

That is arguably one of Perl's most famous features!

Regular expresions (Perl's own) are very similar to what you would do with
sed, if you are familiar with that.

open IN,  "< $input"  or die "Unable to open $input for reading, $!,
stopped";
open OUT, "> $output" or die "Unable to open $output for writing, $!,
stopped";

print "Munging $input, creating new file $output.\n";


while (  ) {
s/oldtext/newtext/g;  # substitute (replace) oldtext with newtext,
  # g means globally if happens more than once on a
line.
print OUT;
}

close IN;
close OUT;

print "Munge complete.\n";

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Dan Anderson
Sent: Friday, December 05, 2003 3:11 PM
To: [EMAIL PROTECTED]
Subject: Replacing text



I have a  script that reads text from a  file and inserts text
into different  places depending on  what it needs  to do.  But  I use
split to replace the text, i.e.:

($first_part, $second_part) = split "#INSERT#TEXT#HERE#", $document, 2;
print FILEHANDLE $firstpart, $text_to_insert, $secondpart;

Is  there a  replace function  in perl  that would  let  me do
something like  replace "#INSERT#TEXT#HERE", $text_to_insert;?   I was
going to  write my own  method but was  curious if perl  had something
faster?

Thanks in advance,

-Dan


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Replacing text

2003-12-05 Thread Wagner, David --- Senior Programmer Analyst --- WGO
[EMAIL PROTECTED] wrote:
> I have a  script that reads text from a  file and inserts text
> into different  places depending on  what it needs  to do.  But  I use
> split to replace the text, i.e.:
> 
> ($first_part, $second_part) = split "#INSERT#TEXT#HERE#", $document,
> 2; print FILEHANDLE $firstpart, $text_to_insert, $secondpart;
> 
> Is  there a  replace function  in perl  that would  let  me do
> something like  replace "#INSERT#TEXT#HERE", $text_to_insert;?   I was
> going to  write my own  method but was  curious if perl  had something
> faster?
> 
> Thanks in advance,
> 
> -Dan

You should be able to use a regex(assuming the data you are looking for is in  
$_):

if ( s/#INSERT#TEXT#HERE#/$text_to_insert/ ) {
# success
   }else {
# no hit

   }

Wags ;)


**
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Replacing text

2003-12-05 Thread Philipp Traeder
Hi Dan,

have you tried using a regular expression?
I am using something like this for a similar purpose:



open ($fh, $filename);
# go through each line of the file
while ($_ = <$fh>) {
# replace globally in the default variable $_
s/#INSERT#TEXT#HERE/$text_to_insert/g;
}
close ($fh);



Search for "perlre" in perl-doc for more information.

HTH,

Philipp Traeder

On Sat, 2003-12-06 at 00:10, Dan Anderson wrote:
> I have a  script that reads text from a  file and inserts text
> into different  places depending on  what it needs  to do.  But  I use
> split to replace the text, i.e.:
> 
> ($first_part, $second_part) = split "#INSERT#TEXT#HERE#", $document, 2;
> print FILEHANDLE $firstpart, $text_to_insert, $secondpart;
> 
> Is  there a  replace function  in perl  that would  let  me do
> something like  replace "#INSERT#TEXT#HERE", $text_to_insert;?   I was
> going to  write my own  method but was  curious if perl  had something
> faster?
> 
> Thanks in advance,
> 
> -Dan
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Replacing text

2003-12-05 Thread Dan Anderson

I have a  script that reads text from a  file and inserts text
into different  places depending on  what it needs  to do.  But  I use
split to replace the text, i.e.:

($first_part, $second_part) = split "#INSERT#TEXT#HERE#", $document, 2;
print FILEHANDLE $firstpart, $text_to_insert, $secondpart;

Is  there a  replace function  in perl  that would  let  me do
something like  replace "#INSERT#TEXT#HERE", $text_to_insert;?   I was
going to  write my own  method but was  curious if perl  had something
faster?

Thanks in advance,

-Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: replacing text in a file

2003-06-09 Thread Tassilo von Parseval
On Mon, Jun 09, 2003 at 03:22:52PM +0100 Rob Dixon wrote:

> Tassilo Von Parseval wrote:

> > You can use the same underlying technique from within a Perl script. You
> > have to set two special variables accordingly and Perl can even do an
> > inplace-edit:
> >
> > local $^I = 1;  # enable inplace editing
> > local @ARGV = "blah.txt";   # make it accessible with <>
> > while (<>) {
> > s/blabla/BLABLA/;
> > print;
> > }
> 
> The value of $^I is the string to be appended to the backup copy of
> the original file. The above will edit 'blah.txt' and rename the original
> file to 'blah.txt1'.

Indeed, yes. I wonder how people can memorize that because I've been
getting it wrong for three years. The letter 'i' never fails to trick
me into believing that it's a boolean switch turning on or off
inplace-editing altogether.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~;eval


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: replacing text in a file

2003-06-09 Thread Rob Dixon
Tassilo Von Parseval wrote:
> On Mon, Jun 09, 2003 at 02:46:48AM -0500 christopher j bottaro wrote:
>
> > what is the easiest way to replace text in a file?  say i have file blah.txt
> > and i want to replace some regular expression found in that file with
> > something.  also, i want to do this from within the perl program, not by
> > invoking perl with the -e option.
>
> You can use the same underlying technique from within a Perl script. You
> have to set two special variables accordingly and Perl can even do an
> inplace-edit:
>
> local $^I = 1;  # enable inplace editing
> local @ARGV = "blah.txt";   # make it accessible with <>
> while (<>) {
> s/blabla/BLABLA/;
> print;
> }

The value of $^I is the string to be appended to the backup copy of
the original file. The above will edit 'blah.txt' and rename the original
file to 'blah.txt1'.

> This however might not work on Windows due to some limitations concerning
> open files.

As far as I know this is fine on all systems, including Windows. It will fail
only if $^I is the empty string, which implies editing without a backup and
is not supported by Windows. To disable editing in place $^I must be set to
'undef'. A 'false' value will not do.

> So if the above is no option for you, you have to do it manually:
>
> open IN,  "blah.txt" or die $!;
> open OUT, "blah.txt.tmp" or die $!;

  open OUT, "> blah.txt.tmp" or die $!;

> while () {
> s/blabla/BLABLA/;
> print OUT;
> }
>
> close IN;
> close OUT;
>
> rename "blah.txt.tmp", "blah.txt" or die "Renaming: $!";

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: replacing text in a file

2003-06-09 Thread Dan Muey
> hello,
> what is the easiest way to replace text in a file?  say i 
> have file blah.txt 
> and i want to replace some regular expression found in that file with 
> something.  also, i want to do this from within the perl 
> program, not by 
> invoking perl with the -e option.
#!/usr/bin/perl -w 

use strict;

print `perl -pi -e "s/\b\d+\b/123/ if /^YOURNUMBER/" fix.txt file2.txt joe.html`;


Sorry, I couldn't resist ;p Although that'd still do it from inside your script

Ok depending on the file size ( IE if it's not huge )

use File::Slurp;
my $file = read_file($file); # or @file and do a for() on that array
$file =~ s/Hello/Goodbye/gl
write_file($file);

Or use open() instead of the File::Slurp methods.

HTH

DMuey

> 
> thanks,
> -- christopher
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: replacing text in a file

2003-06-09 Thread Tassilo von Parseval
On Mon, Jun 09, 2003 at 02:46:48AM -0500 christopher j bottaro wrote:

> what is the easiest way to replace text in a file?  say i have file blah.txt 
> and i want to replace some regular expression found in that file with 
> something.  also, i want to do this from within the perl program, not by 
> invoking perl with the -e option.

You can use the same underlying technique from within a Perl script. You
have to set two special variables accordingly and Perl can even do an
inplace-edit:

local $^I = 1;  # enable inplace editing
local @ARGV = "blah.txt";   # make it accessible with <>
while (<>) {
s/blabla/BLABLA/;
print;
}

This however might not work on Windows due to some limitations concerning
open files. 

So if the above is no option for you, you have to do it manually:

open IN,  "blah.txt" or die $!;
open OUT, "blah.txt.tmp" or die $!;

while () {
s/blabla/BLABLA/;
print OUT;
}

close IN;
close OUT;

rename "blah.txt.tmp", "blah.txt" or die "Renaming: $!";

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~;eval


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



replacing text in a file

2003-06-09 Thread christopher j bottaro
hello,
what is the easiest way to replace text in a file?  say i have file blah.txt 
and i want to replace some regular expression found in that file with 
something.  also, i want to do this from within the perl program, not by 
invoking perl with the -e option.

thanks,
-- christopher

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Problem with replacing text in a variable...

2002-04-12 Thread David Gray

> OK!
> 
> my script works! But when I use:
> 
> my $rvar = ;
> 
> I have the same problem! Jesus, that's a hard one :-)

After you read it in, do you have a '$' in the string? If so, you need
to escape it like so:

$rvar =~ s/\$/\\\$/g;

That should fix the problem. If it doesn't, could you paste more of the
code you're using?

 -dave

> >>An update on previous post...
> >>It seems that I typed some errors :-(
> >>
> >>This is a small rewrite of my script...
> >>#!/usr/bin/perl
> >>
> >>use warnings;
> >>
> >>my $var  = "test";
> >>my $rvar = "\$interface";
> >>my $cmd  = "int \$interface\n";
> >>
> >>print "$var\n";
> >>print "$cmd\n";
> >>
> >>$cmd =~ s/$rvar/$var/g;
> >>
> >>print "$cmd\n";
> >>
> >>If you run this, the $interface in the variable $cmd isn't
> >>replaced by 
> >>test...
> >>
> >>Why?
> >>
> >
> >If you change
> >
> >my $rvar = "\$interface";
> >
> >To
> >
> >my $rvar = '\$interface';
> >
> >It works like I think you want it to. The reason it wasn't working 
> >before is because the regex was seeing:
> >
> >$cmd =~ s/$interface/test/;
> >
> >Because when you declared $rvar, it saved the string '$interface', 
> >which then got interpolated by the regular expression.
> >
> >HTH,
> >
> > -dave
> >
> >
> >
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Problem with replacing text in a variable...

2002-04-12 Thread drieux


On Friday, April 12, 2002, at 04:59 , Wim wrote:

> OK!
>
> my script works! But when I use:
>
> my $rvar = ;
>
> I have the same problem! Jesus, that's a hard one :-)

are you expecting a single line of input to come at you?

if not why not 'nest' it in a line read cycle

while() {
chomp;
$rvar=$_;

# do stuff with this line of input from STDIN

}

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Problem with replacing text in a variable...

2002-04-12 Thread Wim

OK!

my script works! But when I use:

my $rvar = ;

I have the same problem! Jesus, that's a hard one :-)


Greetz...

David Gray wrote:

>>An update on previous post...
>>It seems that I typed some errors :-(
>>
>>This is a small rewrite of my script...
>>#!/usr/bin/perl
>>
>>use warnings;
>>
>>my $var  = "test";
>>my $rvar = "\$interface";
>>my $cmd  = "int \$interface\n";
>>
>>print "$var\n";
>>print "$cmd\n";
>>
>>$cmd =~ s/$rvar/$var/g;
>>
>>print "$cmd\n";
>>
>>If you run this, the $interface in the variable $cmd isn't 
>>replaced by 
>>test...
>>
>>Why?
>>
>
>If you change
>
>my $rvar = "\$interface";
>
>To
>
>my $rvar = '\$interface';
>
>It works like I think you want it to. The reason it wasn't working
>before is because the regex was seeing:
>
>$cmd =~ s/$interface/test/;
>
>Because when you declared $rvar, it saved the string '$interface', which
>then got interpolated by the regular expression.
>
>HTH,
>
> -dave
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Problem with replacing text in a variable...

2002-04-11 Thread David Gray

> An update on previous post...
> It seems that I typed some errors :-(
> 
> This is a small rewrite of my script...
> #!/usr/bin/perl
> 
> use warnings;
> 
> my $var  = "test";
> my $rvar = "\$interface";
> my $cmd  = "int \$interface\n";
> 
> print "$var\n";
> print "$cmd\n";
> 
> $cmd =~ s/$rvar/$var/g;
> 
> print "$cmd\n";
> 
> If you run this, the $interface in the variable $cmd isn't 
> replaced by 
> test...
> 
> Why?

If you change

my $rvar = "\$interface";

To

my $rvar = '\$interface';

It works like I think you want it to. The reason it wasn't working
before is because the regex was seeing:

$cmd =~ s/$interface/test/;

Because when you declared $rvar, it saved the string '$interface', which
then got interpolated by the regular expression.

HTH,

 -dave



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Problem with replacing text in a variable...

2002-04-11 Thread bob ackerman


On Thursday, April 11, 2002, at 10:57  AM, wim wrote:

> my $var  = "test";
> my $rvar = "\$interface";
>

try:
my $rvar = '\$interface';

so it doesn't look like a variable to be interpolated when you get to the 
substitution.

> my $cmd  = "int \$interface\n";
>
> print "$var\n";
> print "$cmd\n";
>
> $cmd =~ s/$rvar/$var/g;
>
> print "$cmd\n";



Re: Problem with replacing text in a variable...

2002-04-11 Thread wim

An update on previous post...
It seems that I typed some errors :-(

This is a small rewrite of my script...
#!/usr/bin/perl

use warnings;

my $var  = "test";
my $rvar = "\$interface";
my $cmd  = "int \$interface\n";

print "$var\n";
print "$cmd\n";

$cmd =~ s/$rvar/$var/g;

print "$cmd\n";

If you run this, the $interface in the variable $cmd isn't replaced by 
test...

Why?

Greetz, and thanks for the great help!

Wim


David Gray wrote:

>>I have a problem with substitutions...
>>In the piece of code, here below,
>>"display $var1, $var"' should be "display val1, val2";
>>I don't see why, but the if condition doesn't seems to 
>>work... and if I remove the if condition, the replacement 
>>doesn't occur... Does any of you know why?
>>
>>$VarValue{$var1} = "val1";
>>$VarValue{$var2} = "val2"
>>$Cmd = "display $var1, $var2";
>>
>
>The previous line evaluates to 'display , ' when the perl interpreter
>hits it because it looks for the variables $var1 and $var2, but can't
>find them, so it uses '' (the string value of undef) instead. You might
>want to try:
>
>$Cmd = 'display $var1, $var2';
>
>Same thing for the hash you're creating... If you want to do what I
>think you want to do, you need to define that hash as:
>
>$VarValue{'$var1'} = 'val1';
>$VarValue{'$var2'} = 'val2';
>
>HTH,
>
> -dave
>
>
>




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Problem with replacing text in a variable...

2002-04-11 Thread David Gray

> I have a problem with substitutions...
> In the piece of code, here below,
> "display $var1, $var"' should be "display val1, val2";
> I don't see why, but the if condition doesn't seems to 
> work... and if I remove the if condition, the replacement 
> doesn't occur... Does any of you know why?
> 
> $VarValue{$var1} = "val1";
> $VarValue{$var2} = "val2"
> $Cmd = "display $var1, $var2";

The previous line evaluates to 'display , ' when the perl interpreter
hits it because it looks for the variables $var1 and $var2, but can't
find them, so it uses '' (the string value of undef) instead. You might
want to try:

$Cmd = 'display $var1, $var2';

Same thing for the hash you're creating... If you want to do what I
think you want to do, you need to define that hash as:

$VarValue{'$var1'} = 'val1';
$VarValue{'$var2'} = 'val2';

HTH,

 -dave



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Re: Problem with replacing text in a variable...

2002-04-11 Thread p

Wim,

Sorry, I forgot to mention escaping/single quoting the hash keys too...

As you defined it, your hash is empty as you are trying to set 'val1', 'val2' to 
VarValue{undef} ($Var1 hasn't been defined).
Try
VarValue{'$Var1'}
or 
VarValue{"\$Var1"}
Now the string '$Val1' is a the key, not undef. (assuming you can have hash keys that 
start with dollar signs)

Make sense? :)

I think using the -w switch and 'use strict' would have sorted this out, because you'd 
have had to explicity declared all of your variables, so using $Var1 (which you 
wouldn't have declared) would have caused an error.

Tristan


You Wrote:
--

Tristan,

I'm writing a script to send command from a textfile to cisco devices...

I the textfile, I like to have variables, something like

conf t
int $interface
..

when my script reads the line, it should replace $interface by the value 
asked from the value the user enters...
$var{$interface} contains that value...
in the previous mail, $var has nothing to do with perl ;-)

Sorry,
 I wasn't clear (in my head ;-) )


[EMAIL PROTECTED] wrote:

>Wim,
>
>You're not declaring $var1 or $var2 anywhere. Instead you're assigning 'val1' and 
>'val2' to elements in the hash %VarValue.
>
>So something like...
>$VarValue{$var1} = 'val1';
>print "I like $VarValue{$var1}!";
>
>will print out...
>I like val1
>
>To define $var1, do something like:
>$var1 = 'cheese';
>
>Now $var1 is 'cheese', and $VarValue{$var1} is the value of %VarValue with a key of 
>'cheese'.
>
>My head hurts!
>
>Tristan
>
>
>p.s. What are you trying to do? I.e. what's that code for? It looks like a mighty 
>confusing way of doing anything. Replacing a hash key with it's hash value seems like 
>a bad idea to me. 
>
>
>You Wrote:
>--
>
>Hello people from the mighty list,
>
>I have a problem with substitutions...
>In the piece of code, here below,
>"display $var1, $var"' should be "display val1, val2";
>I don't see why, but the if condition doesn't seems to work...
>and if I remove the if condition, the replacement doesn't occur...
>Does any of you know why?
>
>$VarValue{$var1} = "val1";
>$VarValue{$var2} = "val2"
>$Cmd = "display $var1, $var2";
>
>foreach $Tmp ( sort keys %VarValue ) {
>if ( $Cmd =~ /$Tmp/ ) {
>print "* In command $Cmd, replace $Tmp by $VarValue{$Tmp}\n";
>$Cmd =~ s/$Tmp/$VarValue{$Tmp}/g;
>}
>}
>
>
>
>Thanx a lot!
>
>Wim
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






___
sent via the murky.net webmail hegemony

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Re: Problem with replacing text in a variable...

2002-04-11 Thread p

Wim,

I can't believe how long it's taken me to figure out what you're on about, and what 
the problem (probably) is. My brain's far too slow to be at work today :)

If cmd came from standard input or something, your script would work. But you're using 
a string to simulate that input, and because it's in double quotes perl is trying to 
interpolate any variables it finds. Like me, it thinks you mean $var1 and $var2 are 
variables. Try escaping them, or single quoting it.
$cmd = 'Display $var1, $var2';
or
$cmd = "Display\$var1, \$var2';

I *think* that's the problem, but the way my brain is today who the hell knows :)

Tristan




You Wrote:
--

Tristan,

I'm writing a script to send command from a textfile to cisco devices...

I the textfile, I like to have variables, something like

conf t
int $interface
..

when my script reads the line, it should replace $interface by the value 
asked from the value the user enters...
$var{$interface} contains that value...
in the previous mail, $var has nothing to do with perl ;-)

Sorry,
 I wasn't clear (in my head ;-) )

___
sent via the murky.net webmail hegemony

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Problem with replacing text in a variable...

2002-04-11 Thread Wim

Tristan,

I'm writing a script to send command from a textfile to cisco devices...

I the textfile, I like to have variables, something like

conf t
int $interface
.

when my script reads the line, it should replace $interface by the value 
asked from the value the user enters...
$var{$interface} contains that value...
in the previous mail, $var has nothing to do with perl ;-)

Sorry,
 I wasn't clear (in my head ;-) )


[EMAIL PROTECTED] wrote:

>Wim,
>
>You're not declaring $var1 or $var2 anywhere. Instead you're assigning 'val1' and 
>'val2' to elements in the hash %VarValue.
>
>So something like...
>$VarValue{$var1} = 'val1';
>print "I like $VarValue{$var1}!";
>
>will print out...
>I like val1
>
>To define $var1, do something like:
>$var1 = 'cheese';
>
>Now $var1 is 'cheese', and $VarValue{$var1} is the value of %VarValue with a key of 
>'cheese'.
>
>My head hurts!
>
>Tristan
>
>
>p.s. What are you trying to do? I.e. what's that code for? It looks like a mighty 
>confusing way of doing anything. Replacing a hash key with it's hash value seems like 
>a bad idea to me. 
>
>
>You Wrote:
>--
>
>Hello people from the mighty list,
>
>I have a problem with substitutions...
>In the piece of code, here below,
>"display $var1, $var"' should be "display val1, val2";
>I don't see why, but the if condition doesn't seems to work...
>and if I remove the if condition, the replacement doesn't occur...
>Does any of you know why?
>
>$VarValue{$var1} = "val1";
>$VarValue{$var2} = "val2"
>$Cmd = "display $var1, $var2";
>
>foreach $Tmp ( sort keys %VarValue ) {
>if ( $Cmd =~ /$Tmp/ ) {
>print "* In command $Cmd, replace $Tmp by $VarValue{$Tmp}\n";
>$Cmd =~ s/$Tmp/$VarValue{$Tmp}/g;
>}
>}
>
>
>
>Thanx a lot!
>
>Wim
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Problem with replacing text in a variable...

2002-04-11 Thread p

Wim,

You're not declaring $var1 or $var2 anywhere. Instead you're assigning 'val1' and 
'val2' to elements in the hash %VarValue.

So something like...
$VarValue{$var1} = 'val1';
print "I like $VarValue{$var1}!";

will print out...
I like val1

To define $var1, do something like:
$var1 = 'cheese';

Now $var1 is 'cheese', and $VarValue{$var1} is the value of %VarValue with a key of 
'cheese'.

My head hurts!

Tristan


p.s. What are you trying to do? I.e. what's that code for? It looks like a mighty 
confusing way of doing anything. Replacing a hash key with it's hash value seems like 
a bad idea to me. 


You Wrote:
--

Hello people from the mighty list,

I have a problem with substitutions...
In the piece of code, here below,
"display $var1, $var"' should be "display val1, val2";
I don't see why, but the if condition doesn't seems to work...
and if I remove the if condition, the replacement doesn't occur...
Does any of you know why?

$VarValue{$var1} = "val1";
$VarValue{$var2} = "val2"
$Cmd = "display $var1, $var2";

foreach $Tmp ( sort keys %VarValue ) {
if ( $Cmd =~ /$Tmp/ ) {
print "* In command $Cmd, replace $Tmp by $VarValue{$Tmp}\n";
$Cmd =~ s/$Tmp/$VarValue{$Tmp}/g;
}
}



Thanx a lot!

Wim


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







___
sent via the murky.net webmail hegemony

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Problem with replacing text in a variable...

2002-04-11 Thread Wim

Hello people from the mighty list,

I have a problem with substitutions...
In the piece of code, here below,
"display $var1, $var"' should be "display val1, val2";
I don't see why, but the if condition doesn't seems to work...
and if I remove the if condition, the replacement doesn't occur...
Does any of you know why?

$VarValue{$var1} = "val1";
$VarValue{$var2} = "val2"
$Cmd = "display $var1, $var2";

foreach $Tmp ( sort keys %VarValue ) {
if ( $Cmd =~ /$Tmp/ ) {
print "* In command $Cmd, replace $Tmp by $VarValue{$Tmp}\n";
$Cmd =~ s/$Tmp/$VarValue{$Tmp}/g;
}
}



Thanx a lot!

Wim


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Replacing text in only certain sections of a file.

2002-02-08 Thread Jeff 'japhy' Pinyan

On Feb 8, Steven M. Klass said:

>I am having some problems and I can't seem to get it right.  In short, I
>want to only modify text in a specific section.  That section is denoted
>by the following
>

>*DESCRIPTION
>
>
>
>*END
>

This sounds like a job for the .. operator.  Let's see.

>Here is what I have.  I know the problem, I am reading this line by line so 
>when I find the DESCRIPTION I drop into the loop, but because I still have 
>DESCRIPTION obviously I won't search and replace.  
>
>open (RUNSET, $FLOW) || die "Can't access $FLOW[0][1]\n"; 
>while () {
>  if ($_ =~ m/DESCRIPTION/){
>print "Entering description block";
>until ( $_ =~ m/END\.){   

The problem is that you never read from  in this until() loop, so
$_ is never given the new line, and the loop goes on forever.  If you add:

   $_ = ;

here, it might work.

>  print "Entering until";
># next if /;/ # Remove Comments 
>  s/^\ INDISK\s+=.*/  INDISK= $FILENAME ; SMK /;
>  s/^\ PRIMARY\s+=.*/ PRIMARY  = $CELLNAME ; SMK /;
>  }
>  }
>  print TEMPFLOW "$_";
>#  push @runset, $_ ; 
>}

But here's the Perlish way to write it:

  while () {
if (/DESCRIPTION/ .. /END/) {
  # do the following in between a line matching
  # /DESCRIPTION/ and a line matching /END/

  s/^ INDISK\s+=.*/  INDISK= $FILENAME ; SMK /;
  s/^ PRIMARY\s+=.*/ PRIMARY= $CELLNAME ; SMK /;
}
print TEMPFLOW;
  } 

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Replacing text in only certain sections of a file.

2002-02-08 Thread Steven M. Klass

Hi all,

I am having some problems and I can't seem to get it right.  In short, I 
want to only modify text in a specific section.  That section is denoted by 
the following

*DESCRIPTION



*END


Here is what I have.  I know the problem, I am reading this line by line so 
when I find the DESCRIPTION I drop into the loop, but because I still have 
DESCRIPTION obviously I won't search and replace.  

open (RUNSET, $FLOW) || die "Can't access $FLOW[0][1]\n"; 
while () {
  if ($_ =~ m/DESCRIPTION/){
print "Entering description block";
until ( $_ =~ m/END\.){   
  print "Entering until";
# next if /;/ # Remove Comments 
  s/^\ INDISK\s+=.*/  INDISK= $FILENAME ; SMK /;
  s/^\ PRIMARY\s+=.*/ PRIMARY   = $CELLNAME ; SMK /;
  }
  }
  print TEMPFLOW "$_";
#  push @runset, $_ ; 
}

I know there must be a way to do this.  I know I could get messy by using a 
flag, but I'm sure there is a better way.  Would anyone mind helping me out 
on this?

Thanks

-- 

 Steven M. Klass
 Physical Design Manager

 National Semiconductor Corp
 7400 W. Detroit Street
 Suite 170
 Chandler AZ 85226

 Ph:480-753-2503
 Fax:480-705-6407

 [EMAIL PROTECTED]
 http://www.nsc.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: replacing text, excluding what's between tags.

2001-08-24 Thread Christopher Solomon


I'm not sure exactly what you want to do...

Do you want to replace all of the marked a's:

On Fri, 24 Aug 2001, Danial Magid wrote:

> 
> This is a link to another place
^ ^ ^

or do you just want to replace the word 'a'?


If you just want to replace 'bare' a's then this might work:


$newline =~ s!\s+($pattern)\s+!$1!g


Chris


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




replacing text, excluding what's between tags.

2001-08-24 Thread Danial Magid

HI,

Here is a snippet of an html page..

This is a link to another place

Here is what I am trying to do..

Replace the letter 'a' with the following: "a"

I need to replace the letter 'a', only when it's not within the html tags.

This is what I tried..

$newline = s/[^<.*?]($pattern).*?[^>]/$pattern/g;


TIA for any help.

-Danial 

__
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]