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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值