in reply to perl one liner to search for a string, then search for a different string and append a new line

OK, I'm a little closer. It is finding the correct Directory section, it is printing the CustomLog line, but the Directory section is not printed out. That is, in the httpd.conf file I have:
# # This is to permit URL access to scripts/files in AWStats directory. # <Directory "/usr/local/awstats/wwwroot"> Options None AllowOverride None Order allow,deny Allow from all </Directory>
My code is: perl -0777 -elnp 's/<Directory "\/usr\/local\/awstats.+<\/Directory>/$1\nCustomLog \/usr\/local\/awstats\/logs\/access_log comdinedio/s' httpd.conf What I get out is:
# # This is to permit URL access to scripts/files in AWStats directory. # CustomLog /usr/local/awstats/logs/access_log combinedio ?
Any idea how to get the matched text back? Thanks!
  • Comment on Re: perl one liner to search for a string, then search for a different string and append a new line
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: perl one liner to search for a string, then search for a different string and append a new line
by Athanasius (Archbishop) on Dec 20, 2012 at 13:37 UTC

    You need to put capturing parentheses around the text to be matched, in order to initialise $1.

    You should also definitely follow uncoolbob’s advice and use a non-greedy quantifier.

    And your code will be much easier to read if you use different delimiters, because then you won’t have to backslash all the forward slashes (“leaning toothpick syndrome”):

    perl -0777 -elnp 's{(<Directory "/usr/local/awstats.+?</Directory>}{$1 +\nCustomLog /usr/local/awstats/logs/access_log comdinedio}s' httpd.co +nf

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,