linux 系统中如何统计文件列数

1、测试数据

[root@centos79 test]# cat a.txt
e r w i
s g n c
w d h x

 

2、awk

[root@centos79 test]# cat a.txt
e r w i
s g n c
w d h x
[root@centos79 test]# awk '{print NF}' a.txt
4
4
4
[root@centos79 test]# awk 'END{print NF}' a.txt
4

 

3、grep + wc -l

[root@centos79 test]# cat a.txt
e r w i
s g n c
w d h x
[root@centos79 test]# head -n 1 a.txt | grep -o " " | sed "1i xx" | wc -l
4

 

4、tr + wc -l

[root@centos79 test]# cat a.txt
e r w i
s g n c
w d h x
[root@centos79 test]# head -n 1 a.txt | tr " " "\n" | wc -l
4

 

5、

root@DESKTOP-1N42TVH:/home/test2# cat a.txt
e r w i
s g n c
w d h x
root@DESKTOP-1N42TVH:/home/test2# line1=(`head -n 1 a.txt `)  ## 生成一维数组
root@DESKTOP-1N42TVH:/home/test2# column_line1=${#line1[@]}   ## 统计一维数组的长度
root@DESKTOP-1N42TVH:/home/test2# echo $column_line1
4

 

posted @ 2021-07-26 00:26  小鲨鱼2018  阅读(1289)  评论(0编辑  收藏  举报