double search and replace

T

text news

I have already posted a little earlier today "Extract text to html href" and
please forgive this second post but I have given it some thought and this
may be a little more focused.
It is the same problem but maybe a much simpler approach.

I need to do an advanced search and replace on a large number of files.
The search string is easy and identical in every file but the replace string
will be different on every file, although it can be easily identified and
exists in each file.

For example:

text of file 1.

"photo1
blah blah blah blah
xxxxx"

text of file 2.

"photo2
blah blah blah blah
xxxxx"

text of file 3.

"photo3
blah blah blah blah
xxxxx"

I want to replace xxxxx in each case with photo1, photo2 etc.

I could do this with wild cards but need a kind of double search.
i.e. search for photo# and make this the replace string.
now search for xxxxx and replace it with the result of the first search.

That must make more sense than my earlier posting.

Thanks
Les
 
J

Jim

Try this Perl script:


# usage: perl thisscript.pl < oldFile >newFile
#
# feed this file to itself to see the substitions in
# the lines at the end fo the file.

# Loop over each line of input looking for
# replacement strings and substituing the replacement
# string for strings of x's.
#
while( <> )
{
#set the replacement string to whatever matches
# 'photo' followed by and number ofdigits.
# The match will assign whatever matched the the
# contents of the parens to '$1'.
#
$replacement = $1 if ( m/(photo[0-9]+)[^0-9]/ );

substitute one or more 'x' with the replacement.
s/x+/$replacement/;

# print out the line.
print;
}


#rest of file is a string so perl not complain
"

text of file 1.

"photo1
blah blah blah blah
xxxxx"

text of file 2.

"photo2
blah blah blah blah
xxxxx"

text of file 3.

"photo3
blah blah blah blah
xxxxx"

text of file 99.

"photo99
blah blah blah blah
xxxxx"

";
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top