Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To remove duplicates, just store second and third fields in a hash (as a key) when you read the line. Then, print in file 1 or in file 2 depending on whether the key already exists in the hash.

Something like this (untested):

my %already_seen; while (<>) { my @temp = split /\s\, $_; my $key = $temp[1,2]; if defined ($already_seen{$key} { # it is a duplicate print $output2 $_; } else { $already_seen{$key} = 1; print $output2 $_; } }
Code could be made more concise and the @temp array could be avoided for example with such a syntax:

$key = join " ", (split / /, $_) [1,2];

but I preferred to do it simpler to understand.

Note that this will not work if your input file is huge to the point of getting your hash too large for your memory. The limit will depend on your system, but in general, everything below a million lines should probably work without problem on most current systems. If your file is larger (especially if it is much larger), you might need a different way of doing it.

Similarly, for the order of the 6th column, just store its value in a variable when you read a line and compare 6th column with the variable (i.e. 6th column of previous line).

I leave it to you to put the two rules together.


In reply to Re: move the line if particular column is duplicate or more than 1 entries by Laurent_R
in thread move the line if particular column is duplicate or more than 1 entries by hyans.milis

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found