使用bash读取一个矩阵对角线上的元素,并将其重定向到另一个文件 [英] Using bash to read elements on a diagonal on a matrix and redirecting it to another file
问题描述
所以,目前我已经创建了一个code要做到这一点,如下图所示。这code工作和做的事情是应该做的后,我附和变量:
A =`awk的'NR == 2 {$打印1}$ coor`B =`awk的'NR == 3 {打印$ 2}'$ coor`C =`awk的'NR == 4 {$打印3}$ coor`
....但我必须有更多的行做到这一点,我希望有一个更一般的前pression。所以,我试图创建如下所示的循环。语法明智的,我不认为有什么不妥的code,但它不是文件CMAIN输出任何东西。
我想知道是否有人能帮助我,我有点在脚本新的。
如果它可以帮助任何,我也可以发布什么,我要看书了。
为((i = 1; I< = 4;我++));做
为((J = 0; J&下; = 3; J ++));做
B =`grep的-n细胞,$ COOR| awk的'NR ==$ I{打印附加$ J}'`
DONE
DONE回声$ B>> CMAIN
您可以用这个替换您的 AWK
的行:
的awk'{为(i = 1; I< = NF;我++),如果(NR> = 2及和放大器; NR == I)打印$(I - 1 )}'file.txt的
测试输入:
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
输出:
11
22
33
44
55
66
77
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 {print $1}' $coor`
b=`awk 'NR==3 {print $2}' $coor`
c=`awK 'NR==4 {print $3}' $coor`
....but i have to do this for many more lines and i want a more general expression. So I have attempted to create a loop shown below. Syntax wise i don't think anything is wrong with the code, but it is not outputting anything to the file "Cmain".
I was wondering if anyone could help me, I'm kinda new at scripting.
If it helps any, I can also post what i am trying to read.
for (( i=1; i <= 4 ; i++ )); do
for (( j=0; j <= 3 ; j++ )); do
B="`grep -n "cell" "$coor" | awk 'NR=="$i" {print $j}'`"
done
done
echo "$B" >> Cmain
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
这篇关于使用bash读取一个矩阵对角线上的元素,并将其重定向到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!