linux shell中利用列数筛选数据

1、测试数据,依据列数筛选数据

root@PC1:/home/test# ls
test.txt
root@PC1:/home/test# cat test.txt  ## 测试数据
2 3 4 3 d
a 3 d
d w e f
z v e
z c g k e q
z v b d
root@PC1:/home/test# awk 'NF == 3' test.txt   ## 筛选test.txt中列数为3的数据
a 3 d
z v e
root@PC1:/home/test# awk 'NF == 4' test.txt    ## 筛选test.txt中列数为4的数据
d w e f
z v b d
root@PC1:/home/test# awk 'NF == 5' test.txt
2 3 4 3 d
root@PC1:/home/test# awk 'NF == 6' test.txt
z c g k e q

 

2、

root@PC1:/home/test# cat test.txt
2 3 4 3 d
a 3 d
d w e f
z v e
z c g k e q
z v b d
root@PC1:/home/test# awk '{if(gsub(/ /, "&") == 3 - 1) print $0}' test.txt   ## 提取列数为3的行
a 3 d
z v e
root@PC1:/home/test# awk '{if(gsub(/ /, "&") == 4 - 1) print $0}' test.txt
d w e f
z v b d
root@PC1:/home/test# awk '{if(gsub(/ /, "&") == 5 - 1) print $0}' test.txt
2 3 4 3 d

 

posted @ 2021-12-16 22:34  小鲨鱼2018  阅读(855)  评论(0编辑  收藏  举报