如何使用awk自定义格式打印.dat文件列

时间:2016-03-25 23:14:17

标签: bash unix awk sed duplicates

我有这个文件

#id|firstName|lastName|gender|birthday|creationDate|locationIP|browserUsed
933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Hồ Chí|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
8333|Chen|Wang|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer

我想找到有计数的dublicate浏览器...所以我需要这样的东西

Firefox 1
Internet Explorer 3

我正在使用此代码,但结果不是我想要的

代码:

awk -F '|' '!/^($|[:space:]*#)/{ print $8}' $3 | sort  | uniq -c | awk '{print $2 , $1}'

结果:

Firefox 1
Internet 3

我能做什么呢?我可以拥有整个“Internet Explorer”和单词的计数权吗?

1 个答案:

答案 0 :(得分:3)

awk救援!

$ awk -F'|' 'NR>1{c[$NF]++} END{for(k in c) print k, c[k]}' file 

说明:

NR>1跳过标题

c[$NF]++计算数组c中不同的最后字段

最后

END{..迭代数组c中的键并打印键和 计数