我在文件中有一行如下所示:
$db['foo']['database'] = 'bar';
我想使用ack或grep或其他东西从该字符串中返回bar
。到目前为止,我有:
ack '^\$db\['\''foo'\''\]\['\''database'\''\] = '\''([\w_]+)'\' $file
但是不知道如何让它只吐出第一个反向引用,而不是全线。
答案 0 :(得分:0)
也许perl
可以提供帮助:
script.pl
的内容:
use warnings;
use strict;
while ( <> ) {
printf qq[%s\n], $1 if m/\A\$db\['foo'\]\['database'\]\s*=\s*'([^']+)'.*\Z/;
}
infile
的内容:
$db['foo']['database'] = 'bar';
像以下一样运行:
perl script.pl infile
使用以下输出:
bar