0

I have a line in a file that looks like this:

$db['foo']['database'] = 'bar';

I want to use ack or grep or something to return bar out of that string. So far I have:

ack '^\$db\['\''foo'\''\]\['\''database'\''\] = '\''([\w_]+)'\' $file

But don't know how to get it to spit out just the first backreference, instead of the whole line.

futuraprime
  • 5,468
  • 7
  • 38
  • 58
  • Never mind: this is answered in http://stackoverflow.com/questions/4222727/how-to-use-named-regex-groups-in-ack-output – futuraprime Apr 24 '12 at 16:22

1 Answers1

0

Perhaps perl can help:

Content of script.pl:

use warnings;
use strict;

while ( <> ) {
        printf qq[%s\n], $1 if m/\A\$db\['foo'\]\['database'\]\s*=\s*'([^']+)'.*\Z/;
}

Content of infile:

$db['foo']['database'] = 'bar';

Run it like:

perl script.pl infile

With following output:

bar
Birei
  • 35,723
  • 2
  • 77
  • 82