So like I said before, you should make an SSCCE. You can use the __DATA__ section to include sample data and read from it as a filehandle. Then after you get your data processing code working, you can add back in the file opening and reading part. (But I highly recommend using Path::Tiny for all your file handling.)

The following script uses data stored within it for the input file, then prints the output to a file in the temp directory, then reads the file in and verifies that it is as expected.

(Note: Because of the technique I use of printing the output to the output file with the same line separator used to break the input into "paragraphs," the string is restored to the end of each line printed, so, given that the first line never had it removed, the result is that each line begins with the same initial string as in the original file. This has the side effect of leaving the string (">sp") alone on the last line of the file.)

use strict; use warnings; use feature 'say'; use Path::Tiny; use Test::More; # When not testing, something like this: # my $in_file = path("/path/to/file"); # my $fh = $in_file->openr; my $script = path(__FILE__); my $out_file = path( sprintf('/tmp/%s-%s.log', $script->basename('.pl' +), time) ); my %seen; my ($count_in, $count_out); { # set the line separator locally to a string that matches # the break between your "paragraphs". local $/ = "\n>sp"; while ( my $line = <DATA> ) { # <$fh> $count_in++; if ( $line =~ /GN=(\S*)/ ) { next if $seen{$1}; $count_out++; $seen{$1} = 1; $out_file->append( $line ); } } } say ' Records processed: ' . $count_in; say 'Duplicates removed: ' . ($count_in - $count_out); #----------------------------------------------------# ## Test. Always test. is( $count_in, 5, 'Input read OK'); is( $count_out, 3, 'Correct number of records kept'); my @written = $out_file->lines; is( (grep { /GN/ } @written), 3, 'File has correct count for +"GN"'); for my $value (qw/ TUFA Blorgle Blargle /) { is( (grep { /$value/ } @written), 1, sprintf('File has correct co +unt for "%s"', $value) ); } done_testing; __DATA__ >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=TUFA PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blorgle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|Q43467|EFTU1_SOYBN Elongation factor Tu, chloroplastic OS=Glycine +max OX=3847 GN=TUFA PE=3 SV=1 MAVSSATASSKLILLPHASSSSSLNSTPFRSSTTNTHKLTP This is a dupe but has extra content >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blargle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blorgle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR
Script output:
$ perl 11103674.pl Records processed: 5 Duplicates removed: 2 ok 1 - Input read OK ok 2 - Correct number of records kept ok 3 - File has correct count for "GN" ok 4 - File has correct count for "TUFA" ok 5 - File has correct count for "Blorgle" ok 6 - File has correct count for "Blargle" 1..6

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re^4: remove entries with duplicate characters by 1nickt
in thread remove entries with duplicate characters by davi54

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.