1

Greetings Stack Overflow community.

I'm currently struggling with a relatively simple thing. To automatically find and log Application configurations, I am currently working on a script that reads out configuration data from the config file of certain applications. Since these config files can vary in syntax, and the only constant is the pattern of for example db names, I want to extract these db names though a regex.

For Example:

The DB names always have the syntax db_s{serverid}_{dbid}

where the serverid consists of 2-4 numbers, and the database id of 2-4 numbers, counting up. valid DB names would be for example:

db_s0001_01
db_s1337_42
db_s123_123
db_s42_1337

Database Names are stored in config files with varying syntax, depending on the used Application. Here are some examples:

define( 'DB_NAME', 'db_s0001_01' );
define( 'DB_NAME', "db_s0001_01" );
'dbname' => 'db_s0001_01',
'dbname' => "db_s0001_01",
database_name: db_s0001_01

Breaking down the config files to get the right lines for the right credential variables is not the problem. But how would I go on and extract the precise database-name out of the config-strings?

The Regex should be something like

(db_s[0-9]{2,4}_[0-9]{2,4})

But I can't quite get behind which tool to use and how to extract the exact Database name. So how would get only the DB name out of this?

echo 'define( 'DB_NAME', "db_s0001_01" );' | grep/sed/awk (db_s[0-9]{2,4}_[0-9]{2,4})
1
  • Single quotes don't nest. Try the echo without any pipe, it doesn't print what you expect. Commented Aug 14, 2020 at 8:45

1 Answer 1

2

As mentioned in the comments single quote characters don't nest. So the input you're testing against is incorrect. After fixing that, I would use sed to extract the DB names out like so:

echo "define( 'DB_NAME', \"db_s0001_01\" );" | sed 's/.*\(db_s[0-9]\{2,4\}_[0-9]\{2,4\}\).*/\1/g'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.