Using bash to read elements on a diagonal on a matrix and redirecting it to another file

前端 未结 3 1825
无人及你
无人及你 2021-01-21 01:57

So, currently i have created a code to do this as shown below. This code works and does what it is supposed to do after I echo the variables:

a=`awk \'NR==2 {pri         


        
相关标签:
3条回答
  • 2021-01-21 02:14

    awk 'BEGIN {f=1} {print $f; f=f+1}' infile > outfile

    0 讨论(0)
  • 2021-01-21 02:19

    An alternative using sed and coreutils, assuming space separated input is in infile:

    n=$(wc -l infile | cut -d' ' -f1)
    for i in $(seq 1 $n); do
      sed -n "${i} {p; q}" infile | cut -d' ' -f$i
    done
    
    0 讨论(0)
  • 2021-01-21 02:22

    You can replace your lines of awk with this one:

    awk '{ for (i=1; i<=NF; i++) if (NR >= 2 && NR == i) print $(i - 1) }' file.txt
    

    Tested input:

    1 2 3 4 5 6 7 8 9 10
    11 12 13 14 15 16 17 18 19 20
    21 22 23 24 25 26 27 28 29 30
    31 32 33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48 49 50
    51 52 53 54 55 56 57 58 59 60
    61 62 63 64 65 66 67 68 69 70
    71 72 73 74 75 76 77 78 79 80
    

    Output:

    11
    22
    33
    44
    55
    66
    77
    
    0 讨论(0)
提交回复
热议问题