关于awk中通过管道执行shell后的管道关闭问题

原文:http://hi.baidu.com/leejun_2005/blog/item/88f7b9838e794785f703a60b.html

1、先来看个小例子:

$ echo "21 2
> 3 51
> 17 23"|awk '{
>       first[NR]=$1
>       second[NR]=$2
> }END{
>       print "====打印第 1 列并排序:====" >> "testAwkPipe.txt"
>       for(i in first){
>               print first[i]|"sort -n >> testAwkPipe.txt"
>       }
>       print "====打印第 2 列并排序:====" >> "testAwkPipe.txt"
>       for(j in second){
>               print second[j]|"sort -n >> testAwkPipe.txt"
>       }
> }'
$ cat testAwkPipe.txt
====打印第 1 列并排序:====
2
3
17
21
23
51
====打印第 2 列并排序:====
$

怎么样?结果是不是很诡异呢?这是因为我们在awk中使用了管道后没有close的缘故,这样前一次管道里的数据和文件描述符(指针)还建立着连接,因而,每次排序都是全文排序,下面我们在每个for循环外面加上一句close来关闭管道,看看结果如何:

$ echo "21 2
> 3 51
> 17 23"|awk '{
>       first[NR]=$1
>       second[NR]=$2
> }END{
>       print "====打印第 1 列并排序:====" >> "testAwkPipe.txt"
>       for(i in first){
>               print first[i]|"sort -n >> testAwkPipe.txt"
>       }
>      close("sort -n >> testAwkPipe.txt")
>       print "====打印第 2 列并排序:====" >> "testAwkPipe.txt"
>       for(j in second){
>               print second[j]|"sort -n >> testAwkPipe.txt"
        }
       close("sort -n >> testAwkPipe.txt")
}'> > >
$ cat testAwkPipe.txt
====打印第 1 列并排序:====
3
17
21
====打印第 2 列并排序:====
2
23
51
$

现在的结果就是正确的了,这是因为for循环结束后管道里的数据已经和文件断开了连接,每次排序的数据和前一次无关。

最后我们来看看 man bash手册里的权威解释吧:

close(file [, how])   Close file, pipe or co-process.  The optional how should only be used when closing one end of a two-way pipe  to
                             a co-process.  It must be a string value, either "to" or "from".
system(cmd-line)      Execute the command cmd-line, and return the exit status.  (This may not be available on non-POSIX systems.)
       fflush([file])        Flush any buffers associated with the open output file or pipe file.  If file is missing, then  standard  output
                             is flushed.  If file is the null string, then all open output files and pipes have their buffers flushed.
       Additional output redirections are allowed for print and printf.
       print ... >> file
              Appends output to the file.
       print ... | command
              Writes on a pipe.
       print ... |& command
              Sends data to a co-process or socket.  (See also the subsection Special File Names, below.)
       The getline command returns 1 on success, 0 on end of file, and -1 on an error.  Upon an error, ERRNO contains a string describing the
       problem.
NOTE: Failure in opening a two-way socket will result in a non-fatal error being returned to the calling function. If  using  a  pipe,
       co-process,  or  socket to getline, or from print or printf within a loop, you must use close() to create new instances of the command
       or socket.  AWK does not automatically close pipes, sockets, or co-processes when they return EOF.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22980662/viewspace-729988/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22980662/viewspace-729988/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值