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

 

posted @ 2021-01-30 11:57  小鲨鱼2018  阅读(6344)  评论(0编辑  收藏  举报