Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return string between single quote with awk

Tags:

shell

awk

I'm currently writing a script to mass reset wordpress username and passwords. The passwords were stored using old-passwords and now with an update to MySQL and PHP, the old passwords are no longer working and hence need to be reset.

I have a file that looks like this:

define('DB_USER', 'abc_12345');
define('DB_PASSWORD', 'abc12345');
define('DB_USER', 'def_34589');
define('DB_PASSWORD', 'def34589');

I'm just needing to return the values inside the single quotes:

DB_USER abc_12345
DB_PASSWORD abc12345

DB_USER def_34589
DB_PASSWORD def34589

Now, I've looked around at the similar questions, but they all are a bit different then what I'm working with.

I've tried the following:

cat file.txt | awk '{print $1,$2}' 

which returns

define('DB_USER', 'abc_12345');
define('DB_PASSWORD', 'abc12345');
define('DB_USER', 'def_34589');
define('DB_PASSWORD', 'def34589');

I've tried

cat file.txt | awk '{print $1}'

which returns

define('DB_USER',
define('DB_PASSWORD',

I've tried

cat file.txt | awk '{print $2}'

which returns

'abc_12345');
'abc12345');

I've also tried awk mixed with grep

cat file.txt | awk '{print $2}' | grep -P " '*.' "

which doesn't return anything.

like image 813
Jimmy Avatar asked Dec 15 '22 22:12

Jimmy


1 Answers

awk -F\' '{print $2,$4}' file.txt
like image 199
Jean-Pierre Avatar answered Jan 02 '23 18:01

Jean-Pierre