1

I'm trying to continue to extract and isolate sections of text within my wordpress config file via bash script. Can someone help me figure out my sytax?

The lineof code in the wp-config.php file is:

$table_prefix  = 'xyz_';

This is what I'm trying to use to extract the xyz_ portion.

prefix=$(sed -n "s/$table_prefix = *'[^']*'/p" wp-config.php)
echo -n "$prefix"

There's something wrong with my characters obviously. Any help would be much appreciated!

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
sven30
  • 403
  • 2
  • 8
  • 15

2 Answers2

3
  1. Your sed command is malformed. You can use s/regex/replacement/p to print your sed command. Yours, as written, will give unterminated 's' command. If you want to print your whole line out, you can use the capture group \0 to match it as s/<our_pattern>/\0/p
  2. Bash interpets $table_prefix as a variable, and because it is in double quotes, it tries to expand it. Unless you set this variable to something, it expands to nothing. This would cause your sed command to match much more liberally, and we can fix it by escaping the $ as \$table_prefix.
  3. Next, this won't actually match. Your line has multiple spaces before the =, so we need another wildcard there as in ...prefix *= *...
  4. Lastly, to extract the xyz_ portion alone, we'll need to do some things. First, we have to make sure our pattern matches the whole line, so that when we substitute, the rest of the line won't be kept. We can do this by wrapping our pattern to match in ^.* ... .*\$. Next, we want to wrap the target section in a capture group. In sed, this is done with \(<stuff>\). The zeroth capture group is the whole line, and then capture groups are numbered in the order the parentheses appear. this means we can do \([^']*\) to grab that section, and \1 to output it:

All that gives us:

prefix=$(sed -n "s/^.*\$table_prefix *= *'\([^']*\)'.*\$/\1/p" wp-config.php)
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
  • I've never seen `\0` used like that in sed and it doesn't seem to be documented, but it works! Interesting. – Benjamin W. Aug 21 '20 at 03:27
  • Btw, why not use single quotes so you don't have to escape things? – Benjamin W. Aug 21 '20 at 03:29
  • 1
    The single quotes he wants to reference in the pattern to match mean that we'd have to replace 3 instances of `'` with `'\''` and I decided that looked uglier and I didn't want to explain string concatenation and single quote escaping vs. having to escape two `$`. – jeremysprofile Aug 21 '20 at 15:09
-1

The only issue with the regex is that the '$' character specifies that you are using a bash variable and since the pattern is wrapped in double quotes (", bash will attempt to expand the variable. You can mitigate this by either escapping the $ or wrapping the pattern in single quotes and escaping the single quotes in the pattern

Lastly, you are using the sed command s which stands for subsitute. It takes a pattern and replaces the matches with text in the form of s/<pattern>/<replace>/. You can omit the 's' and leave the 'p' or print command at the end. After all your command should look something like:

sed -n "/\$table_prefix = *'[^']*'/p" wp-config.php
joshmeranda
  • 3,001
  • 2
  • 10
  • 24
  • your answer is incorrect, and cites the wrong reasons. `$table_prefix` is first being interpreted by bash as a variable and being expanded into whatever that variable is set to (likely, nothing). Also, because you omit the `-n`, all lines will be printed regardless, and you mistyped the filename so even fixing all that will not work. – jeremysprofile Aug 21 '20 at 02:57
  • You're absolutely right about the variable expansion and variable file name, but even so the regex would be correct (I'll update the answer). I'm almost certain that anyone using this command would understand to not just copy and paste blindly and use the command without thinking about how to use it. – joshmeranda Aug 21 '20 at 03:16
  • You guys are so smart! Thanks for the help. – sven30 Aug 21 '20 at 03:49