Linux 中awk命令如何统计每行指定字符出现的次数

 

001、

[root@localhost test]# ls
a.txt
[root@localhost test]# cat a.txt                                       ## 测试数据
0aa1 0AA2 03 04
05 06 07 08
09 10 1a1 12
[root@localhost test]# awk '{a = gsub("[Aa]", ""); print a}' a.txt     ## 统计每行中A或者a出现的次数
4
0
1

 

image

 。

 

002、

[root@localhost test]# ls
a.txt
[root@localhost test]# cat a.txt                                        ## 测试数据
0aa1 0AA2 03 04
05 06 07 08
09 10 1a1 12
[root@localhost test]# awk -F "[Aa]" '{print NF - 1}' a.txt             ## 指定输入分割符,利用分割的字段来统计
4
0
1
[root@localhost test]# awk 'BEGIN{FS = "A|a"} {print NF - 1}' a.txt     ## 指定输入分隔符
4
0
1

image

 。

 

posted @ 2025-10-01 21:00  小鲨鱼2018  阅读(12)  评论(0)    收藏  举报