menu replacing

menu replacing

am 14.09.2007 15:35:47 von 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



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

Re: menu replacing

am 14.09.2007 19:08:11 von nobull67

On Sep 14, 2:35 pm, jimmy wrote:
> 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
>


>
> 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 (//) {
$in_menu = 0;
print unless $in_menu;
if ( // ) {
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.

Re: menu replacing

am 15.09.2007 11:34:56 von Michal Nazarewicz

> On Sep 14, 2:35 pm, jimmy wrote:
>> this is what i intend to do there is a menu on about 25 lines inside
>> the tags
>>
>>


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

Brian McCauley writes:
> 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 (//) {
> $in_menu = 0;
> print unless $in_menu;
> if ( // ) {
> print "First new line\n";
> print "Second new line\n";
> $in_menu = 1;
> }

The code is missing some "}". Here's fixed version with some more
possible modification examples:

#v+
my $in_menu = 0;
while (<>) {
if ($in_menu) {
if (//) {
$in_menu = 0;
print "First new line at the end\n";
print "Second new line at the end\n";
} else {
next if /line you want to delete/;
next if /another line you want to delete/;
s/something you want to replace/with something else/;
}
} elsif (//) {
$in_menu = 1;
print;
print "First new line\n";
print "Second new line\n";
next;
}
print;
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--