Show "uniq -c" results only for more than X occurrences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Show "uniq -c" results only for more than X occurrences
# 1  
Old 02-28-2013
Show "uniq -c" results only for more than X occurrences

Say I have test.txt with the following data:
Code:
user1 mailbox11 IP1
user1 mailbox12 IP2
user2 mailbox21 IP1
user3 mailbox31 IP1
user1 mailbox11 IP1
user1 mailbox11 IP1
user1 mailbox11 IP1
user1 mailbox12 IP2
user2 mailbox21 IP1
user2 mailbox21 IP1
user2 mailbox21 IP1
user2 mailbox21 IP1
user2 mailbox21 IP1
user3 mailbox31 IP1

Code:
~$ cat test.txt | uniq -c | sort -rn
      5 user2 mailbox21 IP1
      3 user1 mailbox11 IP1
      1 user3 mailbox31 IP1
      1 user3 mailbox31 IP1
      1 user2 mailbox21 IP1
      1 user1 mailbox12 IP2
      1 user1 mailbox12 IP2
      1 user1 mailbox11 IP1

I want my result to be:

Code:
~$ cat test.txt | uniq -c | sort -rn
      5 user2 mailbox21 IP1

because that would be the only result with >=5 (X) occurrences.

I could use head to get top occurrences, but that is not specific enough for me.

Thanks.

Last edited by Franklin52; 02-28-2013 at 10:39 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 02-28-2013
Here is a solution using awk program:
Code:
awk '{ a[$0]++ } END { for ( i in a ) { if ( a[i] >= 5 ) print a[i], i } } ' test.txt

OR
Code:
sort test.txt | uniq -c | while read o l
do
  [[ $o -ge 5 ]] && echo "$o $l"
done

# 3  
Old 02-28-2013
Thanks. I actually came up with:
Code:
cat test.txt | uniq -c | sort -rn | awk 'int($1)>=5'

5 can be replaced with any value

Last edited by Franklin52; 03-01-2013 at 03:12 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 02-28-2013
Quote:
Originally Posted by striker4o
Thanks. I actually came up with:
cat test.txt | uniq -c | sort -rn | awk 'int($1)>=5'
5 can be replaced with any value
Please note that you have to sort first and then do uniq -c else you will not get accurate results.

Also no need to use cat because sort is capable of reading file thus save a pipeline:
Code:
sort test.txt | uniq -c | awk 'int($1)>=5'

# 5  
Old 02-28-2013
The example is some dummy command, what I have is a bit more complicated Smilie. Thanks for the suggestions though.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

4. Red Hat

"/usr/sbin/hpacucli ctrl all show" command does not work

Dear Concern, We have observed that following command stuck/does not work in some RedHat nodes. Please advise us to troubleshoot the issue. /usr/sbin/hpacucli ctrl all show Note: HP Array Configuration Utility CLI for Linux 64-bit With Best Regards, Md. Abdullah-Al Kauser (3 Replies)
Discussion started by: makauser
3 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Difference between plain "uniq" and "uniq -u"

Dear all, It's not entirely clear to me from manpage the difference between them. Why we still need "-u" flag? - monkfan (3 Replies)
Discussion started by: monkfan
3 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies
Login or Register to Ask a Question