linux系统中统计每一行的字符数及总字符数

 

1、awk命令

[root@PC3 test]# cat a.txt
dfs
dsafd
d
fgasdf
safd
ge
[root@PC3 test]# awk -F "" '{print NF}' a.txt
3
5
1
6
4
2

 

2、

[root@PC3 test]# cat a.txt
dfs
dsafd
d
fgasdfd
safd
ge
[root@PC3 test]# a=`awk 'END{print NR}' a.txt `
[root@PC3 test]# echo $a
6
[root@PC3 test]# for i in `seq $a`;do sed -n "$i"p a.txt | wc -c | awk '{print $0-1}';done
3
5
1
7
4
2

 

3、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
dfs
dsafd
d
fgasdf
safd
ge
root@PC1:/home/test2# awk '{print length($0)}' a.txt
3
5
1
6
4
2
root@PC1:/home/test2# awk '{print length($0) + 1}' a.txt    ## 包含末尾的换行符
4
6
2
7
5
3

 

4、统计文本的总字符数

001、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
dfs
dsafd
d
fgasdf
safd
ge
root@PC1:/home/test2# wc -c a.txt     ## 包含每一行末尾的换行符
27 a.txt

 

002、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
dfs
dsafd
d
fgasdf
safd
ge
root@PC1:/home/test2# awk '{sum += length($0) + 1} END {print sum}' a.txt    ## 包含每一行末尾的换行符
27
root@PC1:/home/test2# awk '{sum += length($0)} END {print sum}' a.txt       ## 不包含每一行末尾的换行符
21

 

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