微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。