How do I filter or retain duplicates in Perl?

Posted by manu on Stack Overflow See other posts from Stack Overflow or by manu
Published on 2010-05-07T09:49:58Z Indexed on 2010/05/07 14:28 UTC
Read the original article Hit count: 156

Filed under:

I have one text string which is having some duplicate characters (FFGGHHJKL). These can be made unique by using the positive lookahead:

$ perl -pe 's/(.)(?=.*?\1)//g']

For example, with "FFEEDDCCGG", the output is "FEDCG".

My question is how to make it work on the numbers (Ex. 212 212 43 43 5689 6689 5689 71 81 === output should be 212 43 5689 6689 71 81) ? Also if we want to have only duplicate records to be given as the output from a file having n rows

212 212 43 43 5689 6689 5689 71 81
66 66 67 68 69 69 69 71 71 52
..

Output:

212 212 43 43 5689 5689
66 66 69 69 69 71 71

How can I do this?

© Stack Overflow or respective owner

Related posts about perl