我有一个如下所示的文件,我正在获取$2列上的最后一个数字,并计算有多少条记录从0到9结束,但有时没有0到9的记录,所以我需要将其替换为结果为零。我的意思是,当我在 $2 上没有任何记录时,例如用数字 2 完成时,我会将结果数字 2 --> 0
输入文件示例:
2022-11-17 05:00:02.327, MSG:86442, AppID:22
2022-11-17 05:00:10.829, MSG:81874, AppID:9
2022-11-17 05:00:14.143, MSG:81778, AppID:10
2022-11-17 05:00:16.365, MSG:81782, AppID:22
2022-11-17 05:00:25.010, MSG:82959, AppID:22
2022-11-17 05:00:30.647, MSG:58653, AppID:22
2022-11-17 05:00:40.852, MSG:58198, AppID:11
2022-11-17 05:00:45.104, MSG:89039, AppID:22
2022-11-17 05:00:45.221, MSG:83564, AppID:21
2022-11-17 05:01:00.618, MSG:34115, AppID:20
2022-11-17 05:01:02.692, MSG:86963, AppID:21
2022-11-17 05:01:02.927, MSG:81387, AppID:10
2022-11-17 05:01:04.826, MSG:82119, AppID:11
2022-11-17 05:01:04.926, MSG:82111, AppID:11
2022-11-17 05:01:04.945, MSG:82116, AppID:13
2022-11-17 05:01:00.618, MSG:59110, AppID:20
输出:
# awk -F ',' '{print $2}' test.log|cut -c 10-10|sort|uniq -c
1 0
1 1
2 2
2 3
2 4
1 5
1 6
1 7
2 8
3 9
我尝试如下所示,但我需要将零放入字段,其中我在 $2 末尾没有找到任何记录(0 到 9):
2022-11-17 05:00:02.327, MSG:86442, AppID:22
2022-11-17 05:00:14.143, MSG:81778, AppID:10
2022-11-17 05:00:16.365, MSG:81782, AppID:22
2022-11-17 05:00:25.010, MSG:82959, AppID:22
2022-11-17 05:00:40.852, MSG:58198, AppID:11
2022-11-17 05:00:45.104, MSG:89039, AppID:22
2022-11-17 05:01:00.618, MSG:34115, AppID:20
2022-11-17 05:01:02.927, MSG:81387, AppID:10
2022-11-17 05:01:04.826, MSG:82119, AppID:11
2022-11-17 05:01:04.926, MSG:82111, AppID:11
2022-11-17 05:01:04.945, MSG:82116, AppID:13
2022-11-17 05:01:00.618, MSG:59110, AppID:20
Output expectation :
nuberofrecords N°从0到9
# awk -F ',' '{print $2}' test.log|cut -c 10-10|sort|uniq -c
nuberofrecords N°from0 to 9
1 0
1 1
2 2
**0 3**
**0 4**
1 5
1 6
1 7
2 8
3 9
您可以尝试这个
awk
命令:
awk -F, '
{ freq[substr($2,length($2))]++ }
END { for (i=0; i<=9; ++i) print freq[i]+0, i }
' file