user3583029
user3583029

Reputation: 59

How to Custom Format Print .dat file column with awk

I've got this file

#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

And i want to find dublicate browsers with count...So i need something like this

Firefox 1
Internet Explorer 3

I am using this code but the result is not what i want

Code:

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

Result:

Firefox 1
Internet 3

What can i do so i can have whole "Internet Explorer" and the count right of the words ?

Upvotes: 1

Views: 226

Answers (1)

karakfa
karakfa

Reputation: 67507

awk to the rescue!

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

Explanation:

NR>1 skip the header

c[$NF]++ count distinct last fields in array c

END{.. at the end iterate the keys in array c and print key and counts

Upvotes: 3

Related Questions