Solved

how to subsitutes strings with variable values in perl

Posted on 2009-07-01
14
196 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:ommer
  • 6
  • 4
  • 4
14 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24757104
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
 

Author Comment

by:ommer
ID: 24757226
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24757253
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
 

Author Comment

by:ommer
ID: 24757323
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
 
LVL 84

Expert Comment

by:ozo
ID: 24757389
#is this what you are trying to do?
s/page(\d+)/page@{[++$pageseed]}/g;
0
 

Author Comment

by:ommer
ID: 24757536
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24757609
IN that case you can just use a single variable:

$pageSeed = 3;

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


That work?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ommer
ID: 24757793

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
 
LVL 84

Expert Comment

by:ozo
ID: 24757824
#if "page" must occur at the beginning of  a line
s/^(page)\d+/$1.$pageseed++/gme;
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24757851
my ($Page_Seed)=3;
while (<$in>)
{
s/\b(page)\d+/$1.$pageseed++/ge;
print $out $_;
}
0
 

Author Comment

by:ommer
ID: 24758128
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
 
LVL 84

Expert Comment

by:ozo
ID: 24758198
#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
 

Author Closing Comment

by:ommer
ID: 31598900
thanks!
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24758286
Thanks for your time. ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now