• 欢迎光临~

linux awk命令取矩阵对角线元素

开发技术 开发技术 2022-01-23 99次浏览

1、取正对角线

root@PC1:/home/test# ls
a.txt
root@PC1:/home/test# cat a.txt
1 D E
2 s d
3 d c
root@PC1:/home/test# awk '{print $NR}' a.txt   ## 取矩阵对角线元素,正对角线
1
s
c

 

2、取逆对角线

root@PC1:/home/test# ls
a.txt
root@PC1:/home/test# cat a.txt
1 D E
2 s d
3 d c
root@PC1:/home/test# awk '{for(i = NF; i >= 1; i--) printf("%s ", $i); printf("n")}' a.txt | awk '{print $NR}'  ## 取逆对角线元素
E
s
3
root@PC1:/home/test# ls
a.txt
root@PC1:/home/test# cat a.txt
1 D E
2 s d
3 d c
root@PC1:/home/test# awk '{print $(NF + 1 - NR)}' a.txt   ## 取逆对角线元素
E
s
3

 

程序员灯塔
转载请注明原文链接:linux awk命令取矩阵对角线元素
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com