[英]How to use awk counting the number of specificed digit in certain column
How can I count the matched value in the certain column? 如何计算特定列中的匹配值?
I have a file:(wm.csv) 我有一个文件:(wm.csv)
I executed the command to get the targeted value in certain column: tail -n +497 wm.csv | awk -F"," '$2=="2" {print $3" "$4}'
我执行了命令以在某些列中获取目标值: tail -n +497 wm.csv | awk -F"," '$2=="2" {print $3" "$4}'
tail -n +497 wm.csv | awk -F"," '$2=="2" {print $3" "$4}'
then I get the following output data I want: 然后得到以下所需的输出数据:
hit 2 hit 2 hit 2 hit 2 miss hit 2 hit 2 hit 2 hit 2 hit 2 hit 2 hit 2 incorrect 1 hit 2 hit 2 hit 2
I want to count the number of "2" in second column in order to do simple math like: total digits in column divided by total number of row. 我想计算第二列中的“ 2”数,以便进行简单的数学运算,例如:列中的总位数除以行的总数。 Specifically, in this case, it would looks like: 14 (fourteen "2" in second column) / 16 (total number of row) 具体来说,在这种情况下,它看起来像是: 14 (第二列中有十四个“ 2”) / 16 (行总数)
Following is the command I tried but this does not work : tail -n +497 wm.csv | awk -F"," '$2=="2" {count=0;} { if ($4 == "2") count+=1 } {print $3,$4,$count }'
以下是我尝试过的命令,但这不起作用: tail -n +497 wm.csv | awk -F"," '$2=="2" {count=0;} { if ($4 == "2") count+=1 } {print $3,$4,$count }'
tail -n +497 wm.csv | awk -F"," '$2=="2" {count=0;} { if ($4 == "2") count+=1 } {print $3,$4,$count }'
thanks 谢谢
taking the posted data as input file 将过帐的数据作为输入文件
$ awk '$2==2{c++} END{print NR,c,c/NR}' file
16 14 0.875
awk '($0 ~ "hit 2"){count += 1} END{print count, FNR, count/FNR}' sample.csv
14 16 0.875
I use ~
to compare the whole line($0) matches "hit 2", if it is increase counter by 1. FNR
is the file number of records which is the total line number. 我用~
比较整行($ 0)匹配“ hit 2”,如果它是将计数器加FNR
是记录的文件号,即总行号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.