awk 无法让我的 for i 循环工作
我正在尝试在日志文件中循环并添加其中一个字段的出现次数。
我知道字段 #8 将包含我想要添加的出现次数,并且该字段将包含 1 到 924 之间的数字。
到目前为止,我有这个 awk 单行:
awk '{count[$8]++}END{for(j in count) print j, count[j]" HIT"}' myfile.txt
但是,我希望 awk 输出以下数字:女巫它没有找到出现的地方并在旁边打印 0。
例如:
1 5 HIT
2 0 HIT
3 55 HIT
我尝试过这个:
awk '{for(i=1;i<=924;i++) print i, count[$8]++}' myfile.txt
编辑:我也尝试过这个
awk '{count[$8]++}END{for(i=1;i<925;i++) print i, count[i]" HIT"}' myfile.txt
它给了我这个:
919 HIT
920 HIT
921 HIT
922 HIT
923 HIT
924 HIT
我确信所有这些都有计数。
任何帮助将不胜感激!
I'm trying to loop in a log file and add occurrences of one of the field.
I know that the field #8 will have the occurrences I want to add, and that the field will contain a number between 1 and 924.
So far I had this awk one-liner:
awk '{count[$8]++}END{for(j in count) print j, count[j]" HIT"}' myfile.txt
But, I would like awk to output the numbers for witch it didn't find occurrences and print 0 next to it.
For example:
1 5 HIT
2 0 HIT
3 55 HIT
I tried this:
awk '{for(i=1;i<=924;i++) print i, count[$8]++}' myfile.txt
EDIT: I also tried this
awk '{count[$8]++}END{for(i=1;i<925;i++) print i, count[i]" HIT"}' myfile.txt
It gave me this:
919 HIT
920 HIT
921 HIT
922 HIT
923 HIT
924 HIT
And I'm sure there are counts for all of those.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
我希望这有帮助。
PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。
try this
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.