Bash printing diagonal of any size array

后端 未结 3 1784
傲寒
傲寒 2021-01-15 03:38

So here I have the following code:

#!/bin/bash
awk \'BEGIN {f=4} {printf($f\" \"); f=f-1}\'

Which will take input such as:

         


        
3条回答
  •  无人及你
    2021-01-15 04:32

    $ awk '{print $(NF+1-NR)}' file    
    4
    7
    1
    4
    
    $ awk -v ORS=" " '{print $(NF+1-NR)}' file
    4 7 1 4 
    

    or if you want to avoid adding a space to the end of your output line and to have a terminating newline:

    $ awk '{printf "%s%s", (NR>1?FS:""), $(NF+1-NR)} END{print ""}' file
    4 7 1 4
    

提交回复
热议问题