Here's a quick and dirty solution:

#!/usr/bin/perl use strict; use warnings; my @lines = (); my %numbers = (); while(<DATA>) { my ($number) = m/^(\d+)/; push @lines, [$number, $_]; $numbers{$number}++; } foreach (@lines) { print $_->[1] if $numbers{$_->[0]} > 1; } __DATA__ 1 ahjewgfje 1 gopjregre 2 kkkkkkk 3 figjiorger 3 rekopfroeer 3 ejfjviknced 4 erjgirjgerio 5 eieuiee 5 reopjtfrpeoi

What this does is iterate through the data (from the special DATA filehandle), extract the number at the beginning of each line using a regular expression (in list context, so it returns the captured values), and populate an array of arrays where each element of the first array is an anonymous two-element array containing the extracted number and the entire line. It also keeps a running total of how often each number was seen.

Once all that's done it goes through the array of arrays; for each element ($_, representing a line), it checks whether the extracted number ($_->[0], the first element of the anonymous array currently being looked at) has a running total of more than one, and if so, prints the line in question ($_->[1], the second element of the anonymous array).

One downside is that this'll slurp the entire file into memory before printing anything, which may be a problem if your files are very large.

It'll also work no matter whether lines with the same number are separated by lines with different numbers or whether they're not (as in your sample data). Whether this is a feature or a bug only you can say.


In reply to Re: Remove unique lines from file by AppleFritter
in thread Remove unique lines from file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.