3

I have a file with three columns and I would like to get duplicated lines by third column, for example:

AAA = 342  
BLABLABLA = 2  
BBBx2 = 23  
1+1 = 2  
KOKOKO= 5  
2x1 = 2  

The output sould be:

BLABLABLA = 2  
1+1 = 2  
2x1 = 2  

I tryed with sort and uniq, but it deletes the repeated lines and I want to print them.

5

As long as you're interested in the last column, you can do it with sort and uniq:

$ sort -k3n test.txt |  uniq  -f2 -D
1+1 = 2
2x1 = 2
BLABLABLA = 2

Here, the sort option -k3n causes the file to be sorted starting with the third field, in numeric order; the options to uniq are:

-f2   Skip the first two fields before checking for uniqueness
-D    Print all the repeated lines

Unfortunately, you cannot control the number of fields to be checked for uniqueness. You can use -w to specify a number of characters to check, but that will only help if the field you are interested in is fixed-width.

Also, beware of trailing whitespace. It will be included in the text to be checked for uniqueness.

0

Use Uniq.

uniq -f 3 <file name>
  • This is wrong. It prints the unique lines and not the duplicate lines. – tommy.carstensen Dec 20 '18 at 16:21
0

@rici 's response is correct, but note that -D is a GNU extension

Here's an awk solution. It runs in a single pass, but stores the previous line:

sort -k3n <file name> | awk 'a[$3]++{ if(a[$3]==2){ print b }; print $0}; {b=$0}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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