I'm pretty good at using php's preg_match
(and similar) commands, and I'm also pretty good with regular expressions, but I don't do very well with sed
.
I have two shell scripts I'm working on and I'd like to be able to pull some variables out of configuration files.
First File
The first file is an .htaccess
file and I want to grab the web address, which is going to be in a block that looks like this:
RewriteCond %{HTTP_HOST} !^www\.mysite\.net$
RewriteRule (.*) http://www.mysite.net/$1 [R=301,L]
The syntax of the .htaccess
file is going to be pretty regular so I feel like I should use a pattern similar to #.*(http.*?)\$#is
which does:
- regular expression
- delimited by #
- 0 or more characters before http
- start capturing at the http with the non-greedy symbol ?
- continue capturing until you hit an actual dollar sign (escaped)
- match case insensitive
- ignore whitespace / newlines
How can I use that regular expressionwith a command like sed
so that I get the part inside parenthesis if it matches, and nothing (empty string) if it does not match?
Would I be better off using another command besides sed
if I am used to php's PCRE?
Second File
The second file is a little different because it is an .ini
file and so I wonder if there might be some shell magic (I use bash
) in order to parse it. The chunk I want looks like this:
[Database]
database = mysql://user:password@localhost/database
If I was using PHP and regular expressions I would do something like this:
#\s+database\s*=\s*mysql://([\:]+):([\@]+)@([\/]+)/(.*?)\s+#is
In PHP there is an .ini parser, but I want this to be a shell/bash script, not a PHP script
How can I use that regular expression to get the database connect credentials?