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 FileThe 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 expressiondelimited by #0 or more characters before httpstart capturing at the http with the non-greedy symbol ?continue capturing until you hit an actual dollar sign (escaped)match case insensitiveignore whitespace / newlinesHow 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 FileThe 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/databaseIf I was using PHP and regular expressions I would do something like this:#\s+database\s*=\s*mysql://([\:]+):([\@]+)@([\/]+)/(.*?)\s+#isIn PHP there is an .ini parser, but I want this to be a shell/bash script, not a PHP scriptHow can I use that regular expression to get the database connect credentials?
How can I find matches with sed (or similar) for configuration parameters?
bash;shell;scripting;sed;regular expression
If you want to use perl-like regular expressions, why not use perl (which is found on 99% of non-embedded Unices)?Like:perl -lne 'print for /(http.*?)\$/'perl -lne 'print for m{database\s*=\s*mysql://([^:@]+):([^@]+)@([^/]+)/(\S+)}i'/.../ is the shorter form of m/.../. The m/.../ one allows other characters than / and also pairs like m{...}, m(...).something for @list runs something looping as the $_ variable on the values of the list. m{...} in list contexts returns the list of captured patterns (inside (...)). print without arguments, prints $_.