how to subsitutes strings with variable values in perl

Hi, all perl experts,
I need your help on this regular expression that will replace the page numbers with a newer set of numbers.

Here is part of my input file
------ ---- ----
page1
thehe
page2
the
junk
page1

and next is the output file I want to produce.
------ ---- ----
page3
thehe
page4
the
junk
page5

The code I had could only replace the string "page". I need syntax helps on
1) find the page AND the number comes after them,
2) replace them with "page" NewNo which is the $Page_Seed plus 1 , and,
3) a machanisim to loop through each of this "page" label.

Thanks!

my ($Page_Seed)=3;
while (<$in>)
{
s/\b(page)\b/pageNewNo/g;
print $out $_;
}

Open in new window

ommerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
Something like this?
#!/usr/bin/perl
 
my $s = "page1";
 
$s =~ s/^page(\d+)/sprintf "page%d", $1+1/e;
 
print "$s\n";

Open in new window

0
ommerAuthor Commented:
your code made my screen printed
page2
page3
...
page28
It was driven by the number of lines I had from my input file. Why it is not by the number of "page"s found?
0
mrjoltcolaCommented:
Not sure. This code below prints as I expect it:

page2
page2
page6

Post your code?
#!/usr/bin/perl
 
my $s = "page1\npage1\npage5";
 
$s =~ s/^page(\d+)/sprintf "page%d", $1+1/gme;
 
print "$s\n";

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ommerAuthor Commented:
my ($Page_Seed)=3;
my $s="page1";
open my $in, '<', "file.txt";
open my $out, '>', "fileNew.txt";
while (<$in>)
{
$s =~ s/^page(\d+)/sprintf "page%d", $1+1/e;
print "$s\n";
#print $out $_;
}
0
ozoCommented:
#is this what you are trying to do?
s/page(\d+)/page@{[++$pageseed]}/g;
0
ommerAuthor Commented:
Yes, #is what I want to see, i.e. all contents from in file with all "page1, page2, page1, ...pageN" lines replaced with "page3, page4, page5, ...pageN+PageSeed" lines.
0
mrjoltcolaCommented:
IN that case you can just use a single variable:

$pageSeed = 3;

$s =~ s/^page(\d+)/sprintf "page%d", ++$pageSeed/gme;


That work?
0
ommerAuthor Commented:

with that and
print $out $s;

I got
page6page7page8page9...page32 in my out file
bty, my page seed this time is 5.

my original 3 steps
1) find the page AND the number comes after them,
page(\d+)  <----------- took care of that
2) replace them with "page" NewNo which is the $Page_Seed plus 1 , and,
sprintf "page%d", ++$pageSeed/gme <------OK??
3) a machanisim to loop through each of this "page" label.
stuck on this part? :(

0
ozoCommented:
#if "page" must occur at the beginning of  a line
s/^(page)\d+/$1.$pageseed++/gme;
0
ozoCommented:
my ($Page_Seed)=3;
while (<$in>)
{
s/\b(page)\d+/$1.$pageseed++/ge;
print $out $_;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ommerAuthor Commented:
this is very close!
I have a minor detail.
On my out file there is a white space between page and the page number. I don't quite understand this $1.$var syntax, so when I tried to add a space there, it did not like it.
thanks!
0
ozoCommented:
#to add a space where there was none before
s/\b(page)\d+/"$1 ".$pageseed++/ge;

#to keep existing spaces from the input
s/\b(page\s*)\d+/$1.$pageseed++/ge;
0
ommerAuthor Commented:
thanks!
0
mrjoltcolaCommented:
Thanks for your time. ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.