需要使用正则expression式/ grep awk / sed / perl模式的帮助

grep输出示例文件

file1:my $dbh = DBI->connect("dbi:mysql:$database_name", $DB_USER, $DB_PASSWD) file2:($dbc,$rc) = mysql_connect($mysql_host,$mysql_user,$mysql_password); 

awk模式应该从第1行获取值databasename,DB_USERDB_PASSWD,并且从第2行获得mysql_host,mysql_user和mysql_password ,即all variables inside the function.

然后它应该在文件之前search该variables的声明:(分号)

例如:file1中的databasename可能是

$ databasename =“dbweb”;

例如:file2中的mysql_user可能是

$ mysql_user =“root”;

结果:它应该显示所有6个variables的variables声明以及文件名

 file2:$mysql_host = "db1"; file2:$mysql_user = "root"; file1:$DB_USER = 'user'; 

这不是awk ,但是这个从stdin读取的Perl脚本应该这样做(或者至less提供一些提示):

 while(<>) { # read stdin chomp; if (/^([^:]+):(.*)$/) { # match filename my $file = $1; my $tail = $2; while ($tail =~ /\$([A-Za-z0-9_]+)/g) { # match each $variable in the line my $varname = $1; open (FILE, $file); while (<FILE>) { chomp; if (/\$$varname\s*=\s*(.*)/ ) { # match the current variable print "$file: \$$varname = $1\n"; } } close (FILE); } } }