Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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';
share|improve this question
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 9 '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 9 '11 at 11:40

1 Answer 1

up vote 2 down vote accepted

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);
        }
    }
}
share|improve this answer
    
Works like a charm, got the idea now.. thanks Lloeki –  Jayakumar K Feb 9 '11 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.