Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: perl one liner to search for a string, then search for a different string and append a new line

by BradV (Sexton)
on Dec 20, 2012 at 13:08 UTC ( [id://1009741]=note: print w/replies, xml ) Need Help??


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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1009741]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found