概括

概括

概括

根据我拥有的bash版本awk,当我尝试对保存awk输出的数组进行切片时,我会得到不同的结果。在旧版本的bash和中, awk“切片操作”(例如${a[@]:2})错误地返回一个包含单个元素的数组,而不管切片数组中剩余多少个元素。但是,这似乎仅在对“捕获”输出的数组进行切片时才会出现问题awk

平均能量损失

  • 您可以运行以下命令来重现该问题:

      bash array_slice.sh text.file
    

    其中array_slice.shtext.file分别为:

  • array_slice.sh脚本:

       #!/usr/bin/env bash                                                        
    
       defaultIFS=$IFS                                                            
       IFS=$'\n'                                                                  
       t=($(awk '{ printf("%s\n", $0) }' "$1"))                                   
       printf "\nArray 't' has ${#t[@]} elements\n"                               
       printf "Elements of array 't':\n"                                          
       printf "%s\n" "${t[@]}"                                                    
    
       t45=( ${t[@]:4} )                                                          
       printf "\nArray 't45' has ${#t45[@]} elements\n"                           
       printf "Elements of array 't45':\n"                                        
       printf "%s\n" "${t45[@]}"      
    
  • text.file

      some text                                                                   
      some other text                                                             
      this is text too                                                            
      text above is text                                                          
      and text below is text text                                                 
      text text  
    

各种平台的输出

请注意,在 Ubuntu 16.04 的输出中数组t45只有一个元素(它应该有两个),而在 Ubuntu 20.04 的输出中它正确地有两个元素。

  • Ubuntu 16.04

    • GNU Awk 4.1.3,API:1.1(GNU MPFR 3.1.4,GNU MP 6.1.0)
    • GNU bash,版本 4.3.48(1)-release(x86_64-pc-linux-gnu)提供:
      Array 't' has 6 elements
      Elements of array 't':
      some text
      some other text
      this is text too
      text above is text
      and text below is text text
      text text
    
      Array 't45' has 1 elements
      Elements of array 't45':
      and text below is text text text text
    
  • Ubuntu 20.04

    • GNU Awk 5.0.1,API:2.0(GNU MPFR 4.0.2,GNU MP 6.2.0)
    • GNU bash,版本 5.0.17(1)-release(x86_64-pc-linux-gnu)提供:
    Array 't' has 6 elements
    Elements of array 't':
    some text
    some other text
    this is text too
    text above is text
    and text below is text text
    text text
    
    Array 't45' has 2 elements
    Elements of array 't45':
    and text below is text text
    text text
    

相关内容