LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2016, 03:26 PM   #1
babyPen
LQ Newbie
 
Registered: May 2014
Posts: 17

Rep: Reputation: Disabled
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,
 
Old 03-10-2016, 03:43 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,876
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
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...

Last edited by astrogeek; 03-10-2016 at 04:05 PM.
 
Old 03-10-2016, 07:36 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,834

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Associative arrays are designed for this - can be done (simply) with one pass of the data in for example awk.
 
Old 03-11-2016, 01:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,247

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You got columns ... you use awk
 
  


Reply

Tags
linux, sort


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get info from a file uniq word count fng77 Linux - Newbie 9 10-21-2015 11:02 PM
how to sort the 2nd column on the basis of first column without repeating the value ? zediok Linux - Newbie 15 12-20-2011 12:48 PM
Warning: [fnn_insert] Column count doesn't match value count at row 1 in bondoq Programming 2 09-27-2011 05:11 PM
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 shifter Programming 2 02-24-2010 08:42 PM


All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration