CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Substitute paragraph

 



JerryBilling
New User

Apr 4, 2014, 1:35 AM

Post #1 of 6 (34911 views)
Substitute paragraph Can't Post

Hi

I want to substitute the following in Apache httpd.conf from a Bash shell script:

<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

with:

# Theese settings are for 3 CPU cores:
<IfModule worker.c>
StartServers 3
MaxClients 450
MinSpareThreads 5
MaxSpareThreads 20
ThreadsPerChild 45
MaxRequestsPerChild 0
ThreadLimit 45
ServerLimit 10
</IfModule>

I have tried to use paragraph matching but have not made it work.
The problem seems to be that there are some comments in the paragraph I want to match and substitute, the original paragraph looks like this:


# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

Any help appriciated!


BillKSmith
Veteran

Apr 4, 2014, 7:10 AM

Post #2 of 6 (34907 views)
Re: [JerryBilling] Substitute paragraph [In reply to] Can't Post

What did you try? This works for me.

Code
use strict; 
use warnings;
my $original = <<"END_ORIG";
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
END_ORIG

my $pattern = <<"END_PATTERN";
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
END_PATTERN

my $replace = <<"END_REPLACE";
# Theese settings are for 3 CPU cores:
<IfModule worker.c>
StartServers 3
MaxClients 450
MinSpareThreads 5
MaxSpareThreads 20
ThreadsPerChild 45
MaxRequestsPerChild 0
ThreadLimit 45
ServerLimit 10
</IfModule>
END_REPLACE

my $expected = <<"END_EXPECTED";
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
# Theese settings are for 3 CPU cores:
<IfModule worker.c>
StartServers 3
MaxClients 450
MinSpareThreads 5
MaxSpareThreads 20
ThreadsPerChild 45
MaxRequestsPerChild 0
ThreadLimit 45
ServerLimit 10
</IfModule>
END_EXPECTED

if ($original =~ s/$pattern/$replace/ms) {
print $original, "\n";
print "\n As expected!\n" if ($original eq $expected);
}
else {
print "No Match/n";
}


OUTPUT:

Code
# worker MPM 
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
# Theese settings are for 3 CPU cores:
<IfModule worker.c>
StartServers 3
MaxClients 450
MinSpareThreads 5
MaxSpareThreads 20
ThreadsPerChild 45
MaxRequestsPerChild 0
ThreadLimit 45
ServerLimit 10
</IfModule>


As expected!

Good Luck,
Bill


JerryBilling
New User

Apr 9, 2014, 6:32 AM

Post #3 of 6 (34560 views)
Re: [BillKSmith] Substitute paragraph [In reply to] Can't Post

Hi Bill!

Thanks for helping me out!

I have written a Bash script that I will use for configuring new Apache servers. I have not figured out how to use your example from my shell script. Should I save your code in a separate file that I execute with perl? The original file I want to change using perl is /etc/httpd/conf/httpd.conf. I have some trouble to translate your example to my situation :)
As you might guees this is my first experiance using Perl.

When I tried to work this out on my own, I tried the following code:

perl -00 -p -i.bak -e -w 's/\# worker MPM
\# StartServers: initial number of server processes to start
\# MaxClients: maximum number of simultaneous client connections
\# MinSpareThreads: minimum number of worker threads which are kept spare
\# MaxSpareThreads: maximum number of worker threads which are kept spare
\# ThreadsPerChild: constant number of worker threads in each server process
\# MaxRequestsPerChild: maximum number of requests a server process serves
\<IfModule worker.c\>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
\<\/IfModule>
/\# StartServers: initial number of server processes to start
\# MaxClients: maximum number of simultaneous client connections
\# MinSpareThreads: minimum number of worker threads which are kept spare
\# MaxSpareThreads: maximum number of worker threads which are kept spare
\# ThreadsPerChild: constant number of worker threads in each server process
\# MaxRequestsPerChild: maximum number of requests a server process serves
\<IfModule worker.c\>
StartServers 3
MaxClients 450
MinSpareThreads 5
MaxSpareThreads 20
ThreadsPerChild 45
MaxRequestsPerChild 0
ThreadLimit 45
ServerLimit 10
\<\/IfModule>
/g' httpd.conf

It worked in with a short snippet of httpd.conf but not with the original httpd.conf


Laurent_R
Veteran / Moderator

Apr 9, 2014, 9:48 AM

Post #4 of 6 (34546 views)
Re: [JerryBilling] Substitute paragraph [In reply to] Can't Post

Yes, save Bill's code into a file suffixed with ".pl" (this is not mandatory but it helps knowing which king of file it is), for example "my_script.pl". Then in your bash script have one line like this:

Code
perl path_of_script/my_script.pl

There are other ways, but this will do.


BillKSmith
Veteran

Apr 9, 2014, 12:03 PM

Post #5 of 6 (34534 views)
Re: [JerryBilling] Substitute paragraph [In reply to] Can't Post

You cannot take Laurent's advice literally. Remember that we are here to help, not to write your perl code and certainly not your Bash code. The code that I posted is a complete program designed to demonstrate that your long strings with many special characters are not a problem for perl's substitute command. It has no file input or output. You must replace my first "here document" with file input and my print with your own file output. I cannot even guess what your error handling should be.

After you have made these changes, you should follow Laurent's advice to integrate it with your Bash.


Note: If your file is relatively small and you know that your pattern will only match one block, you can slurp the entire file into a string and process it with my regular expression. Otherwise, you will need to find a way to parse your file into blocks and process one block at a time. (You have not given un enough info to help with this.)
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 9, 2014, 2:53 PM

Post #6 of 6 (34483 views)
Re: [BillKSmith] Substitute paragraph [In reply to] Can't Post

Oops, I actually knew that your code was more an example than a complete solution, but I did not think about it when I posted my previous message. What I really wanted to do was really just to describe one of the standard ways (perhaps the most standard way) to run Perl code from a shell (Bash or other) script.

One other standard way is to have the script file start with the appropriate "shebang line" (e.g. "#!/usr/bin/perl") and to set the file permissions to execute (say, for example, "chmod 755 filename"). In this case, there is no need to use the "perl" keyword in the bash script. The Bash script line calling the script can just be:

Code
path_of_script/my_script.pl params


Finally, it is also possible to inline Perl code in a Bash (or any other shell) script with something like this:

Code
perl -e ' 
<Perl code here>
' params

but this is acceptable, IMHO, only for very short scripts (let's say less than 10 lines or perhaps even less than 5 lines, depending on the conditions and the programmer's experience), but this should probably not be used by beginners, because it makes debugging significantly more difficult; nobody (or at least no sane person) wants to make the life of beginners more difficult, we all know that starting with a new language is far from being an easy task, there is no reason to make it more difficult.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives