Call Perl script in AWK

I have a problem that I need to call a Perl script with parameters passing through and get the return value of the Perl script in AWK BEGIN. As shown below.

I have a Perl script util.pl

#!/usr/bin/perl -w
$res=`$exe_cmd`;
print $res;

Now in AWK BEGINblock (ksh) I need to call a script and get the return value.

BEGIN { print "in awk, application type is " type;  
                    } \
            {call per script here;}

How do I call a Perl script parameter with a parameter and get the return value $res?

res = util.pl a b c; 
+3
source share
2 answers

Connect the script to getline:

awk 'BEGIN {
         cmd = "util.pl a b c"; 
         cmd | getline res; 
         close(cmd);
         print "in awk, application type is " res
     }'
+2
source

Part of the AWK script I use to retrieve data from the request ldap. Perhaps you can find inspiration in how I do base64 decoding below ...

    /^dn:/{
        if($0 ~ /^dn: /){
            split($0, a, "[:=,]")
            name=a[3]
        }
        else if($0 ~ /^dn::/){
            # Special handling needed since ldap apparently
            # uses base64 encoded strings for *some* users
            cmd = "/usr/bin/base64 -i -d <<< " $2 " 2>/dev/null"
            while ( ( cmd | getline result ) > 0 ) { }
            close(cmd)
            split(result, a, "[:=,]")
            name=a[2]
        }
    }
0
source

All Articles