awk调用外部程序

程序的功能很简单:

  调用外部解密程序decoder,将文件第二列字段解密,然后写入新文件中。

BEGIN { OFS = "\t" }
{    outputFileName = "20170523_" ARGIND ".txt"
    cmd = "./decoder \"" $2 "\""
    while (cmd | getline line) {
        $2 = line
        outputCmd = "tee -a \"" outputFileName "\""
        print $0 | outputCmd
    }
    close(cmd)
}
END { print "Done"}

两个地方值得注意:

1. ARGIND,对应ARGV下标

2. tee命令

// 重定向到文件和屏幕
-a, 向文件重定向时使用追加模式
tee -a result.txt

由于程序耗时较长,因此置于后台运行,同时不再使用tee:

BEGIN { OFS = "\t" }
{    outputFileName = "20170523_" ARGIND ".txt"
    cmd = "./decoder \"" $2 "\""
    while (cmd | getline line) {
        $2 = line
        print $0 > outputFileName
    }
    close(cmd)
}
END { print "Done"}

 脚本启动命令:

awk -f decode.sh *.log &

有一个问题值得注意:

  通过&虽然可以将程序置于后台运行,但是如果你关闭终端的话,程序会挂掉。可以使用nohup命令解决:

nohup awk -f decode.sh *.log &

参考资料:

awk处理多个文件

Linux 技巧:让进程在后台可靠运行的几种方法

转载于:https://www.cnblogs.com/gattaca/p/6903647.html

猜你喜欢

转载自blog.csdn.net/weixin_34212189/article/details/93401881