PDA

View Full Version : [SOLVED] sed and regex help



datakid
February 20th, 2008, 07:38 AM
I noticed the other day that my /var/log/apache2 folder was filling up with heaps and heaps of logs (>1500)

I am not going to stop logging, so please don't offer that as an answer :)

All of my log files are well named (in my mind) eg:

site1-error.log
site1-error.log.0
site1-error.log.1.tgz

site1-access.log
site1-access.log.0
site1-access.log.1.tgz

site2-error.log
site2-error.log.0
site2-error.log.1.tgz

site2-access.log
site2-access.log.0
site2-access.log.1.tgz

etc

So what I wanted to do was to break them into smaller chunks, based on sitename:

/var/log/apache2/site1/error.log
/var/log/apache2/site1/access.log
/var/log/apache2/site2/error.log
/var/log/apache2/site2/access.log

etc

This requires editing a lot of /etc/apache2/sites-available files...

sed '/ErrorLog/ s/\/.*-error.log/&\/error.log/g' bashtest.txt

almost works but gives me:

ErrorLog /var/log/apache2/webalizer-error.log/error.log
ErrorLog /var/log/apache2/wiki-error.log/error.log

instead of the desired

ErrorLog /var/log/apache2/webalizer/error.log
ErrorLog /var/log/apache2/wiki/error.log

I have tried replacing the & with \1 (as I read somewhere), $1 (common in this regard), $0, $@ but none seems to work?

Any ideas? Am I even using the right language? should I be using perl?

datakid
February 20th, 2008, 07:39 AM
Actually, I am aware of the problem with perl:

s/\/.*-error.log/&\/error.log/g'

willl match the whole line (greedy) not just that one little bit....

ghostdog74
February 20th, 2008, 08:06 AM
what is in bashtest.txt?

datakid
February 20th, 2008, 09:11 AM
it's just a list of parts of my vhosts files:

ErrorLog /var/log/apache2/site1-error.log
Errorlog /var/log/apache2/site2-error.log
ErrorLog /var/log/apache2/site3-error.log

CustomLog /var/log/apache2/site1-access.log combined
CustomLog /var/log/apache2/site2-access.log combined
CustomLog /var/log/apache2/site3-access.log combined

I was just using it for testing, later, I do the wrapping script :)

for $i in *
<change text in $i>

then I apply the script to my /etc/apache2/sites-available folder :)

ghostdog74
February 20th, 2008, 09:35 AM
sed '/ErrorLog/ s/\/.*-error.log/&\/error.log/g' bashtest.txt

almost works but gives me:

ErrorLog /var/log/apache2/webalizer-error.log/error.log
ErrorLog /var/log/apache2/wiki-error.log/error.log

instead of the desired

ErrorLog /var/log/apache2/webalizer/error.log
ErrorLog /var/log/apache2/wiki/error.log


I am still not sure what you wish to do, but anyhow, i will just assume you want this


ErrorLog /var/log/apache2/webalizer-error.log/error.log


to become this


ErrorLog /var/log/apache2/webalizer/error.log


You could just substitute -error.log with blank


# sed 's/-error.log//' file
ErrorLog /var/log/apache2/webalizer/error.log
ErrorLog /var/log/apache2/wiki/error.log

datakid
February 21st, 2008, 08:20 AM
of course - thanks for the help.