ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 关于awk中通过管道执行shell后的管道关闭问题

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

原创 Linux操作系统 作者:ciwei_no1 时间:2012-05-17 10:24:59 0 删除 编辑

原文: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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-12-29

  • 博文量
    24
  • 访问量
    55111