I've tried searching but have not found the answer I'm looking for. For this scenario, I have the following script:
#!/bin/bash
CONN="mysql://username:password1@hostname/database_name"
echo "Connection:\t${CONN}"
PW=$(echo ${CONN} |awk '/username:/,/@/')
echo "Username:\t${PW}"
I'm trying to set the new variable of PW to the value of password1.
Is there something I'm missing?
You don't need awk here. Just BASH is good enough:
conn="mysql://username:password1@hostname/database_name"
[[ "$conn" =~ username:(.*)@ ]] && pw="${BASH_REMATCH[1]}"
echo "$pw"
password1
Just do that :
TMP="${CONN##*:}" //Delete all characters from the head till the last ':'
CONN="${TMP%%@*}" //Delete all characters from the tail till the last '@'
and you have password1 in CON
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With