1

Sample file output from 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);

The awk pattern should get values databasename, DB_USER And DB_PASSWD from line 1 and mysql_host,mysql_user and mysql_password from line 2 i.e all variables inside the function.

Then it should search for the declaration of that variable in file before : (semicolon)

ex: databasename in file1 may be

$databasename = "dbweb" ;

ex: mysql_user in file2 may be

$mysql_user="root" ;

Result: It should display variable declarations of all 6 variables along with filenames

file2:$mysql_host = "db1";
file2:$mysql_user = "root";
file1:$DB_USER = 'user';
  • 1
    So, why don't you post your attempt to write this and where it fails, so we can pinpoint where you made an error. Don't expect us to do your work. – Sven Feb 09 '11 at 11:37
  • ":" is a colon, this (";") is a semicolon. Whatever you've done with `grep` can be done directly in an AWK program. – Dennis Williamson Feb 09 '11 at 11:40

1 Answers1

2

That's not awk, but this Perl script reading from stdin should do it (or at least provide some hints):

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);
        }
    }
}
Lloeki
  • 158
  • 6