Home How to make awk grab string in between a second set of single-quotes
Reply: 3

How to make awk grab string in between a second set of single-quotes

Sparky1 Published in 2018-02-12 21:49:42Z

Please help, this is driving me mad.

I've got a standard Wp-config.php file and I'm trying to get awk to output only the database name, database username and password on a single line, but no matter what I try it spits out either irrelevant nonsense or syntax errors.

define('DB_NAME', 'pinkywp_wrdp1');

/** MySQL database username */
define('DB_USER', 'pinkywp_user1');

/** MySQL database password */
define('DB_PASSWORD', 'Mq2uMCLuGvfyw');

Desired output:

pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw

Actual output:

./dbinfo.sh: line 28: unexpected EOF while looking for matching `''
./dbinfo.sh: line 73: syntax error: unexpected end of file
Ed Morton
Ed Morton Reply to 2018-02-12 22:34:55Z
$ awk -F"'" '$1~/^define/ && $2~/^DB_/{ printf "%s%s", $4, (++cnt%3 ? OFS : ORS)}'
pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw
Cyrus Reply to 2018-02-12 22:02:51Z

With GNU awk:

Use ' as field separator and if current line contains 5 columns, print content of column 4 with a trailing blank.

awk -F "'" 'NF==5 {printf("%s ",$4)}' file


pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw 
RomanPerekhrest Reply to 2018-02-12 22:20:15Z

A few awk solutions:

1) with GNU flavor:

awk -v RS="');" '{ printf "%s%s", (NR==1? "":OFS), substr($NF, 2) }END{ print "" }' file

2) quotes-independant solution:

awk -F', ' '/define/{ 
                gsub(/^["\047]|["\047]\);$/, "", $2);
                printf "%s%s", (NR==1? "":" "), $2
            END{ print "" }' file

The output (for both approaches):

pinkywp_wrdp1 pinkywp_user1 Mq2uMCLuGvfyw
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.386472 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO