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:

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.

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:

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

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.