Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Getting most repeated 3 lines

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-10-2013   -   Original Discussion by teefa
teefa teefa is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 10 February 2015, 2:45 PM EST
Posts: 81
Thanks: 31
Thanked 0 Times in 0 Posts
Getting most repeated 3 lines

Hi all
if i want to get the 3 lines of the most repeated lines here

Code:
tony,1,x
tony,1,x
tony,2,x
tony,2,x
tony,3,x
tony,4,x
tony,5,x
adam,1,y

to get output

Code:
tony,1,x
tony,2,x
tony3,x
adam,1,y


Code:
ive tried to uniq -c | /usr/xpg4/bin/awk -F"," '!a[$1]++'

but it gets only first one i need first 3
Sponsored Links
    #2  
Old Unix and Linux 07-10-2013   -   Original Discussion by teefa
vidyadhar85's Unix or Linux Image
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 24 April 2017, 5:14 AM EDT
Location: INDIA, Bangalore
Posts: 2,050
Thanks: 16
Thanked 105 Times in 102 Posts
I am unable to understand your full requirement..
what does this mean? "3 lines of the most repeated lines "
why out of three lines below u selected tony3,x?
tony,3,x
tony,4,x
tony,5,x
Sponsored Links
    #3  
Old Unix and Linux 07-10-2013   -   Original Discussion by teefa
gokcell gokcell is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 1 September 2013, 7:17 AM EDT
Location: Saudi Arabia
Posts: 14
Thanks: 0
Thanked 3 Times in 3 Posts
Count to lines

Hi,

This can help you,


Code:
 cat file1
tony,1,x
tony,1,x
tony,2,x
tony,2,x
tony,3,x
tony,4,x
tony,5,x
adam,1,y


Code:
cat file1 | sort | uniq -c | awk '{print $2"\t""COUNT"$1}'
adam,1,y        COUNT1
tony,1,x        COUNT2
tony,2,x        COUNT2
tony,3,x        COUNT1
tony,4,x        COUNT1
tony,5,x        COUNT1

Then second step


Code:
cat file1 | sort | uniq -c | awk '{print $2"\t""COUNT"$1}' | awk '{print $2}' | sort | uniq
COUNT1
COUNT2

Next step so search COUNT2 parameters


Code:
cat file1 | sort | uniq -c | awk '{print $2"\t""COUNT"$1}' | grep "COUNT2"
tony,1,x        COUNT2
tony,2,x        COUNT2

Unix is life. Always try to find easy way.

Regards,
Goksel Yangin
Computer Engineer
    #4  
Old Unix and Linux 07-10-2013   -   Original Discussion by teefa
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 15 November 2017, 7:27 AM EST
Location: St. Gallen, Switzerland
Posts: 6,574
Thanks: 176
Thanked 560 Times in 481 Posts
You posted nearly the same question here:

https://www.unix.com/shell-programmin...ed-column.html

Please do not open up another thread just because of such an unsignificant difference. Else you get infractions for double posts. Keep that in mind, thanks.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print the lines which are repeated 3 times in a file? ailnilanjan Shell Programming and Scripting 3 05-05-2013 04:11 AM
Remove regularly repeated lines saeed.soltani Shell Programming and Scripting 8 01-05-2013 02:21 AM
Deleting repeated lines by keeping only one. anushree.a Shell Programming and Scripting 2 09-25-2012 07:12 AM
need to print lines between repeated pattern Eman_in_forum Shell Programming and Scripting 13 09-06-2012 11:24 AM
Print specific lines of a repeated set of data morrbie Shell Programming and Scripting 2 03-14-2012 06:34 AM



All times are GMT -4. The time now is 06:40 AM.