1

While I'm learning RegEX, I'm trying to build a bash script to extract data from a file and then store it in variables, so I can work with the stored values to automatize my script.

I know the existence of SED and GREP, but I'm not sure how to use them properly.

The value I need is in a file called: /path/to/config.inc.php

mysql://user:password@server/user_database

So while I was learning RegEX (using the https://regexr.com/ website to learn), I was able to create this RegEX Expression:

(mysql\:\/\/)(.+.)\:(.+.)@(.+.)\/(.+)

So basically I need the USER, PASSWORD and USER_DATABASE values to be stored in the script in variables like:

user = $2
password = $3
userdatabase = $5

So I could call the variables inside the bash script to automatize some stuff. What would be the best approach to that?

2 Answers 2

2

You may use sed + read:

read un pw db < <(sed -En '
s#.*mysql://([^:]+):([^@]+)@[^/]+/([_[:alnum:]]+).*#\1 \2 \3#p' config.inc.php)

# check variable content

declare -p un pw db
declare -- un="user"
declare -- pw="password"
declare -- db="user_database"

RegEx Details:

  • .*mysql://: Match all text till mysql://
  • ([^:]+): Match 1+ non-colon character and capture in group #1
  • :: Match literal colon
  • ([^@]+): Match 1+ non-@ character and capture in group #2
  • @: Match literal @
  • [^/]+/: Match 1+ non-/ character followed by /
  • ([_[:alnum:]]+): Match 1+ word characters and capture in group #2
  • .*: Match any remaining text till end
  • Replacement is \1 \2 \3 which is username password database values in that sequence.
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain me what you regex does and the rest of the script? I thought my regex was okay for this, hahah. And how you define the position for each variable in your example? I don't want to copy and paste your script, I wanna learn from it :)
I have added an explanation
Well... it didn't quite work as expected. I have even imported the text and used your Regex but nothing was selected. When I run the bash script, some weird characters are getting stored in the variables. Do you wanna see by yourself? github.com/roundcube/roundcubemail/blob/master/config/…
1

While the answer with regex is absolutely fine, I think there is another simplier approach to consider. (And I love regex BTW).

Consider this:

mysql://user:password@server/user_database

cut -d"@" -f1

mysql://user:password

cut -d"/" -f3

user:password

cut -d":" -f1

user

The thing is, regex are really cool and useful but can be really hard to understand and maintain for any other person than yourself.

There is a joke going something along these lines:

Have a problem? Use regex to solve it! Now you have two problems ;)

2 Comments

"Now you have two problems", you're right about this, hahaha. But how this cut command work to import the external file? And although it splits quite nice the elements, it doesn't split everything the way I need. Is there a way to tune this command to split each element?
You would have to load the line from the file the way you did before. Be it for example grep: VARIABLE=$(grep -F 'mysql://' yourconfigfile.php or depending on the contents of the config file you could start cutting and assigning to variable like: USER=$(cut -d"@" -f1 config.php | cut -d"/" -f3 | cut -d":" -f1)

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.