I'm working on a bash script and need to use SED and a REGEX to match this line in a text file:
database.system = "pgsql://hostaddr=127.0.0.1 port=5432 dbname=mydb user=myuser password=mypassword options='' application_name='myappname'";
You're using look-ahead assertions in your regular expression (
sed doesn't support.
However, you don't need them, if all you're trying to do is to extract the string inside the double quotes (using GNU
line=$'database.system = "pgsql://hostaddr=127.0.0.1 port=5432 dbname=mydb user=myuser password=mypassword options=\'\' application_name=\'myappname\'";' sed -rn 's/database\.system\s*=\s*"(.*)";/\1/p' <<<"$line"
pgsql://hostaddr=127.0.0.1 port=5432 dbname=mydb user=myuser password=mypassword options='' application_name='myappname'
-r activates support for extended regular expressions, which function (more) like regular expressions in other languages.
-n suppresses printing of each input line by default, so that an explicit output command is needed to produce output.
s/<regex>/<replacement>/p matches each input line against
<regex>, replaces it with
<replacement>, and prints the result (
p), but only if a match was found.
The basic approach is to match the entire line, yet limit the (one and only) capture group to the substring of interest, and then replace the line with only the capture group, which effectively outputs only the substring of interest for each matching line.