You are not logged in.
Basically, I need to make a few modifications to a conf file but I am no good with sed. Here is a portion of the original conf file:
our %HTTP_LOG = ("Linux-RHFC" => "/var/log/httpd/access_log",
"Linux-Debian" => "/var/log/apache2/access.log",
"Linux-Gentoo" => "/var/log/apache2/access_log",
"Linux-Slack" => "/var/log/httpd/access.log",
"Linux-SuSE" => "/var/log/apache2/access_log",
"Linux-Generic" => "/var/log/httpd/access_log",
"FreeBSD" => "/var/log/httpd-access.log");
our %BASE_WWW = ("Linux-RHFC" => "/var/www/html",
"Linux-Debian" => "/var/www",
"Linux-Gentoo" => "/var/www/localhost/htdocs",
"Linux-Slack" => "/var/www/htdocs",
"Linux-SuSE" => "/srv/www/htdocs",
"Linux-Generic" => "/usr/local/www",
"FreeBSD" => "/usr/local/www/apache22/data");I need to make two changes:
1) change the .log to _log in the following:
"Linux-Slack" => "/var/log/httpd/access.log",
2) change the path from "/var/www/htdocs" to "/srv/http" in the following:
"Linux-Slack" => "/var/www/htdocs",
Here is a model line with sed making another needed change that res wrote into the PKGBUILD:
sed '/^our $OSTYPE = "Linux-RHFC"/ s,RHFC,Slack,' < $pkgname.conf \
> $pkgdir/etc/$pkgname.confBasically, I can't adapt his line to do what I need. Anyone willing to help me out? BTW, this is for the monitorix PKGBUILD in the AUR: http://aur.archlinux.org/packages.php?ID=33911
Last edited by graysky (2010-01-26 21:35:28)
Offline
why dont you just make a patch?
cp /path/to/file /path/to/file.new
[make desired changes in file.new]
diff -u /path/to/file /path/to/file.new >patch.diffOffline
CommandLine: (Combined)
sed '/ *"Linux-Slack"/{ s=/var/www=/srv/http=; s=log=_log= }' < infile > outfileOutput:
our %HTTP_LOG = ("Linux-RHFC" => "/var/log/httpd/access_log",
"Linux-Debian" => "/var/log/apache2/access.log",
"Linux-Gentoo" => "/var/log/apache2/access_log",
"Linux-Slack" => "/var/_log/httpd/access.log",
"Linux-SuSE" => "/var/log/apache2/access_log",
"Linux-Generic" => "/var/log/httpd/access_log",
"FreeBSD" => "/var/log/httpd-access.log");our %BASE_WWW = ("Linux-RHFC" => "/var/www/html",
"Linux-Debian" => "/var/www",
"Linux-Gentoo" => "/var/www/localhost/htdocs",
"Linux-Slack" => "/srv/http/htdocs",
"Linux-SuSE" => "/srv/www/htdocs",
"Linux-Generic" => "/usr/local/www",
"FreeBSD" => "/usr/local/www/apache22/data");
Don't try to write to the original file using this approach as it will clobber your original file. Use "sed -i" to save to the original file instead. The "< infile > outfile" save the output to another file obviously.
Sed Script: ( Invoked as sed -f whatever_you_call_the_script file )
/ *"Linux-Slack"/{
s=/var/www=/srv/http=
s=log=_log=
}sed '/^our $OSTYPE = "Linux-RHFC"/ s,RHFC,Slack,' < $pkgname.conf \
> $pkgdir/etc/$pkgname.conf
This means find this regex pattern ^our $OSTYPE = "Linux-RHFC" and whatever it matches change RHFC to Slack. Take $pkgname.conf as the infile and send the output of sed to $pkgdir/etc/$pkgname.conf
Notice he used s,RHFC,Slack, and I used s=log=_log=. This is typically done if you have a lot of "/" in a file. Sed's typical replacement pattern is this s/original/changed/ but as I mentioned it becomes hard to figure out if you have something like this s/\/mnt\/usb\/somefile/\/mnt\/usb\/somename/. Sed allows you to substitute any character (except a few special ones) in place of "/" in it's replacement pattern as long as you follow through the whole command like that. So in essence these are equivalent s,RHFC,Slack,, s=RHFC=Slack=, s/RHFC/Slack/
You did say you wanted to know. Hope this is what you were looking for or at least a step in the right direction. Sorry if I bored you with the school lession.
In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically. --Sherlock Holmes
Offline
My Pleasure! ![]()
In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically. --Sherlock Holmes
Offline
Hi graysky
Well I have to thank you (and res) myself for the wonderful exercise. I'm learning Bash scripting right now and after looking at your PKGBUILD I learned some things about how sed interacts in a Bash script (quotes, -e, \, etc.). I wanted to figure out how to do what you wanted using the way I showed you for a sed script. Bash behaves slightly differently however and this proved a good learning lesson.
Here's what you have:
sed -e '/^our $OSTYPE/ s,RHFC,Slack,' \
-e '/^\s*"Linux-Slack"/ s,/var/log/httpd/access.log,/var/log/httpd/access_log,' \
-e '/^\s*"Linux-Slack"/ s,/var/log/secure,/var/log/auth.log,' \
-e '/^\s*"Linux-Slack"/ s,/var/www/htdocs,/srv/http,' < $pkgname.conf \
> $pkgdir/etc/$pkgname.conf
Which can be further reduced like this:
sed -e '/^our $OSTYPE/ s,RHFC,Slack,' \
-e '/\s*"Linux-Slack"/ {
s,/var/log/httpd/access.log,/var/log/httpd/access_log,
s,/var/log/secure,/var/log/auth.log,
s,/var/www/htdocs,/srv/http, }' < $pkgname.conf > $pkgdir/etc/$pkgname.confInstead of doing a multiple search for the same pattern to change use braces instead. Translated to English it sounds like this. "Look for the specific pattern and when found do this, this, and this." What "res" has sound like this. " Find specific pattern, replace. Find specific pattern again, replace. Find specific pattern a third time and replace." Both ways are correct it just a matter of preference really but the latter way can be faster and way easier to type especially if you have a lot of replacing to do on the same pattern.
If you want give my code a try just download the monitorix.tar.gz and create a small bash/dash script and run it in the un-archived folder. Change in/out file to monitorix.conf and test then do a diff on the two. Comes out the same as res's.
I just threw this up in case you find it interesting. Good luck with your package!!
In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically. --Sherlock Holmes
Offline
Thanks for the tips. You could read a book on sed and still not learn everything it can do.
Offline
Yep, grouping it like that may be convenient at times.
Except when you might be commenting out or otherwise swapping sed lines. You'd still have to mess with new line escaping and prefixing with 'sed -e' as needed, but it's still easier to comment lines when they aren't grouped or as dependent on each other.
Offline
Good Point!
To res:
Usually when I use sed it's for a permanent change so it wouldn't really make a difference but in the context of compiling a package I defiantly see your point. Thanks for the tip back. That's a good lesson about how there many ways to approach a problem. Cool!!
To graysky:
Yep, I've read Sed & Awk about 15 times now and each time I read it I pick something else up. I've also seen (as illustrated above) that there a many tidbits not well documented. You just have to noodle with it till you figure it out.
Thanks guys.
Good Luck
Last edited by harryNID (2010-01-30 10:50:58)
In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically. --Sherlock Holmes
Offline