English 中文(简体)
在 AWK 中调用 Perl 脚本
原标题:Call a Perl script in AWK
  • 时间:2012-05-24 10:09:30
  •  标签:
  • perl
  • awk
  • ksh

我有一个问题, 需要调用 Perl 脚本, 其参数通过, 然后在 AWK < code> BEGIN 块中获取 Perl 脚本的返回值 。 就像下面一样 。

我有一个 Perl 脚本 用户端 pl

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

现在在 AWK BEGIN 块 (ksh) 中,我需要调用脚本并获得返回值。

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

我如何调用带有参数的 Perl 脚本, 并获取 $res 的返回值?

res = util.pl a b c; 
最佳回答

将脚本插入 gingline :

awk  BEGIN {
         cmd = "util.pl a b c"; 
         cmd | getline res; 
         close(cmd);
         print "in awk, application type is " res
     } 
问题回答

用于从 ldap 查询中提取数据的数据的 AWK 脚本 I 的一部分。 或许您可以从下面的 base64 解码方法中找到一些灵感...

    /^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]
        }
    }




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签