gawk高级应用与国际化支持
与其他进程的双向通信
在数据处理中,将数据发送到另一个程序进行处理并读取结果是常见需求。传统方法是使用临时文件,示例代码如下:
# Write the data for processing
tempfile = ("mydata." PROCINFO["pid"])
while (not done with data)
print data | ("subprogram > " tempfile)
close("subprogram > " tempfile)
# Read the results, remove tempfile when done
while ((getline newdata < tempfile) > 0)
process newdata appropriately
close(tempfile)
system("rm " tempfile)
不过这种方法存在不足,它要求程序在用户不可共享的目录中运行,因为可能会出现临时文件名冲突的情况。
而使用gawk,可以通过 |& 操作符打开与另一个进程的双向管道,创建一个协同进程(coprocess)。示例代码如下:
do {
print data |& "subprogram"
"subprogram" |& getline results
} while (data left to process)
cl
超级会员免费看
订阅专栏 解锁全文
3

被折叠的 条评论
为什么被折叠?



