Hello Monks,

My text file contains

wire [130 : 0] dgrs; wire [130 : 0] dgrs; wire [130 : 0] dgrs; wire [130 : 0] grs; wire [130 : 0] grs; wire [130 : 0] grs;
I am removing the duplicate lines
and adding _1, _2,_3 into dgrs and grs such that my text file becomes
wire [130 : 0] dgrs_1; wire [130 : 0] dgrs_2; wire [130 : 0] dgrs_3; wire [130 : 0] grs_1; wire [130 : 0] grs_2; wire [130 : 0] grs_3;
This is my code
#!/usr/bin/perl use strict; use warnings; my $file = 'xyz.txt'; my %seen = (); { local @ARGV = ($file); local $^I = '.bac'; while(<>){ $seen{$_}++; next if $seen{$_} > 1; print; } } print "finished processing file."; my $target_file = 'xyz.txt'; { open my $in_fh, '<', $target_file or die "Cannot open input file: +$!\n"; flock $in_fh, LOCK_EX | LOCK_NB or die "Cannot obtain a lock on $t +arget_file: $!\n"; my $temp_out = File::Temp->new(TEMPLATE => "$0-$$-XXXXX"); while ( my $line = <in_fh> ) { if ( $line =~ /^dgrs/ ) { for my $x (1..3) { print "dgrs$x\n"; } } else { print $line; } } close $in_fh or die "Failed to close $target_file. Aborting. $!\n" +; $temp_out->flush; eval { move($temp_out->filename, $target_file); } or do { warn "Failed to swap $temp_out into $target_file: $!\n"; # There may need to be more cleanup here, or possibly a die is + more appropriate. }; }

I am not getting the output properly. I am beginner to perl and facing some difficulty. Your help is appreciated.

Thanks


In reply to To remove duplicate lines and add digits as per loop by suvendra123

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.