Bash printing diagonal of any size array

后端 未结 3 1814
傲寒
傲寒 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:12

    awk '!i{i=NF}{printf "%s ",$i;i--}'
    

    EDIT: Since Ed has already exploited all the nice and proper awk solutions I'll throw in a native bash solution:

    $ cat t.sh
    #!/bin/bash
    
    while read -ra numbers; do
            : ${i:=${#numbers[@]}}
            diag+=("${numbers[--i]}")
    done < test.txt
    echo "${diag[@]}"
    

    .

    $ ./t.sh
    4 7 1 4
    

提交回复
热议问题