How to sort,uniq and display line that appear more than X times
Asked Answered
S

2

16

I have a file like this:

80.13.178.2
80.13.178.2
80.13.178.2
80.13.178.2
80.13.178.1
80.13.178.3
80.13.178.3
80.13.178.3
80.13.178.4
80.13.178.4
80.13.178.7

I need to display unique entries for repeated line (similar to uniq -d) but only entries that occur more than just twice (twice being an example so flexibility to define the lower limit.)

Output for this example should be like this when looking for entries with three or more occurrences:

80.13.178.2
80.13.178.3
Syconium answered 22/11, 2013 at 14:58 Comment(0)
T
11

With pure awk:

awk '{a[$0]++}END{for(i in a){if(a[i] > 2){print i}}}' a.txt 

It iterates over the file and counts the occurances of every IP. At the end of the file it outputs every IP which occurs more than 2 times.

Toms answered 22/11, 2013 at 15:3 Comment(1)
@Kent Thanks to Bell Labs! :)Toms
E
24

Feed the output from uniq -cd to awk

sort test.file | uniq -cd | awk -v limit=2 '$1 > limit{print $2}'
Effy answered 22/11, 2013 at 15:1 Comment(2)
Perfect! Thank you, this one works for me: cat log.txt | sort | uniq -d -c|awk '$1 > 30{print $2}'Syconium
This is a much cleaner, simpler solution.. Thank youBusily
T
11

With pure awk:

awk '{a[$0]++}END{for(i in a){if(a[i] > 2){print i}}}' a.txt 

It iterates over the file and counts the occurances of every IP. At the end of the file it outputs every IP which occurs more than 2 times.

Toms answered 22/11, 2013 at 15:3 Comment(1)
@Kent Thanks to Bell Labs! :)Toms

© 2022 - 2024 — McMap. All rights reserved.