Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Suppose there is a column of numeric values like following:

File1:

1 
2
3
3
3
4
4
4
5
6

I want the output:

3  
4

That is, only the repeated lines. Is there any command line tools to find this out in Linux? (NB: The values are numerically sorted).

share|improve this question
3  
See man uniq. –  jasonwryan Oct 22 '12 at 5:29

2 Answers 2

up vote 24 down vote accepted

You can use uniq(1) for this:

uniq -d file.txt

This will print out the duplicates only. The input file needs to be sorted such that all duplicates are consecutive (which they appear to be), so run it through sort first if it is not.

share|improve this answer
    
what if I want the triplicates only to be printed? –  MiNdFrEaK Oct 22 '12 at 7:55
3  
@MiNdFrEaK sort | uniq -c | grep '^\s*3\s' | sed 's/^\s*[0-9]*\s*//' for triplicates; replace "3" with any N for N-plicates –  full.stack.ex Oct 22 '12 at 8:10
    
@MiNdFrEaK sort | uniq -c | sed -n 's/^[[:blank:]]*3[[:blank:]]\{1,\}//p' for triplicates –  user24222 Oct 22 '12 at 11:03

Using uniq and awk:

cat File1  | uniq -c | awk '$1 > 1 { print $2 }'
share|improve this answer
1  
This work, but I don't see why you pipe the output of cat? –  Bernhard Oct 22 '12 at 17:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.