JimS JimS - 1 year ago 112
Bash Question

frequency count for file column in bash

I have a file with 8 columns using "|" as a delimiter and I want to count the occurence frequency of the words in the 8th column. I tried awk like this

awk -F '{print $8}' | sort | uniq -c $FILE

but I get instead a print of the whole file and I can't understand what I am doing wrong.

EDIT: Now I get printed what I want as below:


2307 Internet Explorer

369 Safari

2785 Chrome

316 Opera

4182 Firefox

but I can't understand where this "1" come from

Answer Source

You can just awk to do this:

awk -F '|' '{freq[$8]++} END{for (i in freq) print freq[i], i}' file

This awk command uses | as delimiter and uses an array seen with key as $8. When it finds a key $8 increments the frequency (value) by 1. Btw you need to add custom delimiter | in your command and use it like this:

awk -F '|' '{print $8}' file | sort | uniq -c
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download