menu replacing

J

jimmy

Hi all
now this may seem trivial to many here and probably the solution is
embedded in some question asked before but i have zero know how of
perl (have used grep as a command and little searching and extracting)
but i have been working on this website and use some gui based tool
but i guess it will be really faster and efficient doing this using
perl but have no idea.. perl in scripting
this is what i intend to do there is a menu on about 25 lines inside
the tags

<div id="menu"><!--This is the navigation menu-->
-
-
-
-
-
-
-
-

<!-- End Navigation--></div>

only the starting and endline lines (shown here) are constant thru all
the files in all the directories
now i believe these can be used as regular expressing and i can do a
search replace in all the files on all the lines inside these tags
with new lines i have
i saw many posts with features resembling this but dont know if i can
use them for this can
some point to a solution

Thanks

Ps: have already started learning perl.. hope to make it to beginner
level soon
 
B

Brian McCauley

Hi all
now this may seem trivial to many here and probably the solution is
embedded in some question asked before but i have zero know how of
perl (have used grep as a command and little searching and extracting)
but i have been working on this website and use some gui based tool
but i guess it will be really faster and efficient doing this using
perl but have no idea.. perl in scripting
this is what i intend to do there is a menu on about 25 lines inside
the tags
<div id="menu"><!--This is the navigation menu-->
-
-
-
-
-
-
-
-

<!-- End Navigation--></div>

only the starting and endline lines (shown here) are constant thru all
the files in all the directories
now i believe these can be used as regular expressing and i can do a
search replace in all the files on all the lines inside these tags
with new lines i have

If you are happy to simply treat the files as plain (not HTML) this is
easy. But it won't cope unless the file format is strictly controlled.

while (<>) {
if (/<!-- End Navigation-->/) {
$in_menu = 0;
print unless $in_menu;
if ( /<!--This is the navigation menu-->/ ) {
print "First new line\n";
print "Second new line\n";
$in_menu = 1;
}

Note this is deliberately very beginner-ish and/or throw-away code.
And since it's less than 10 lines and intended to be discarded after a
single use I've not even bothered with use strict!

The script as written simply prints the modified file(s) on STDOUT.
With the -i switch on Perl you could make this replace the files. Note
that there's a serious bug in my script in that if there's an unended
menu in any file this script will wipe out the rest of that file and
all remaining files it processes. But again these things don't matter
in a script you will use once and discard.

If you wanted to keep this script and use it again then there are
several things about it you'd probably want to do better.
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top