Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Perl script in AWK

Tags:

ksh

awk

perl

I have a problem that I need to call a Perl script with parameters passing in and get the return value of the Perl script in an AWK BEGIN block. Just like below.

I have a Perl script util.pl

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

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

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

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

res = util.pl a b c; 
like image 256
douyou Avatar asked Feb 26 '26 03:02

douyou


1 Answers

Pipe the script into getline:

awk 'BEGIN {
         cmd = "util.pl a b c"; 
         cmd | getline res; 
         close(cmd);
         print "in awk, application type is " res
     }'
like image 59
Dennis Williamson Avatar answered Feb 27 '26 19:02

Dennis Williamson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!