LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perform a uniq and count on 2nd column (https://www.linuxquestions.org/questions/linux-newbie-8/perform-a-uniq-and-count-on-2nd-column-4175574536/)

babyPen 03-10-2016 02:26 PM

Perform a uniq and count on 2nd column
 
Hello,

I need to perform a unique and count of dups at the same time and it need to be done on the 2nd column in a file.

the delimiter is space, and the file looks like:

column1 column2
abc xyz.com
123 xyz.com
456 xyz.com
peter abc.com
John 123.com
Kyle amazon.com
George amazon.com

The out put to me should look like, something below:

3 xyz.com
1 abc.com
1 123.com
2 amazon.com

I need the uniqueness on column 2 and I want to count the number of occurrence also on column 2.

I tried "sort -u -k2 <filename>" but it does not give me the count of occurrence.

Any thoughts..... help with this is much appreciated.

Thanks,

astrogeek 03-10-2016 02:43 PM

How about this order of operations with suitable flags...

Code:

cut {field spec}| grep {drop heading}| sort | uniq {with count}| sort {count ordering}
Which produces this with your (literal) input file, and final sort added:

Code:

1 123.com
1 abc.com
2 amazon.com
3 xyz.com

I leave the options as an exercise...

syg00 03-10-2016 06:36 PM

Associative arrays are designed for this - can be done (simply) with one pass of the data in for example awk.

grail 03-11-2016 12:21 AM

You got columns ... you use awk :)


All times are GMT -5. The time now is 07:14 PM.