suvendra123 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: To remove duplicate lines and add digits as per loop
by haukex (Archbishop) on Mar 16, 2021 at 08:49 UTC

    It looks like you've copy-and-pasted together code from davido here, myself here, and with $^I from somewhere else (perhaps Duplicate lines with spaces, tabs...). The code you've posted also doesn't compile.

    Do you understand what all of this code is supposed to be doing? I suggest you don't start with this fairly complex code (e.g. do you really need $^I and file locking?), but instead start with perlintro and some simple code.

    We're happy to help you learn Perl. But please understand that what your post currently looks like is that you've copy&pasted code samples together in hopes that someone else will do your work for you. If that's not the case, please share your own thought process and your own efforts.

Re: To remove duplicate lines and add digits as per loop
by hippo (Archbishop) on Mar 15, 2021 at 22:39 UTC

    You have interspersed your code with prose making it more difficult to download and analyse. Do not do that. Wrap your prose in <p> tags and your data and code in <code> tags instead, please.

    Here is a one-liner which does what you want. Add -i to edit in-place (see perlrun)

    perl -lpe '$s{$_}++; s/;$/_$s{$_};/' foo.txt

    🦛

      I don't want to change all lines , only to those lines where string is found

        There are so many ways to achieve that.

        • Change the pattern in the regular expression to match only those lines you want.
        • Make the substitution conditional on your pattern beforehand: /pat/ and s///
        • Don't munge any lines which don't match: next unless /pat/

        Pick one and implement it.


        🦛

Re: To remove duplicate lines and add digits as per loop
by Fletch (Bishop) on Mar 16, 2021 at 20:00 UTC

    Setup a hash to keep a counter for each substituted token. Walk over the lines looking for a match. If it matches, replace with the token followed by the counter value (and increment the counter in your hash); otherwise copy the line.

    Edit: tweaked updating counter hash.

    #!/usr/bin/env bb -i ;; https://github.com/babashka/babashka (defn fix-line [ctrs l] (if-let [[_ pre to-fix] (re-matches #"(.*?\s+)(d?grs);" l)] (do (swap! ctrs update-in [to-fix] #(if % (inc %) 1)) (format "%s%s_%d;" pre to-fix (get @ctrs to-fix))) l)) (let [ctrs (atom {})] (doseq [l *input*] (println (fix-line ctrs l))))
    $ ./fwee.clj < terrible_sample.txt 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

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: To remove duplicate lines and add digits as per loop
by BillKSmith (Monsignor) on Mar 16, 2021 at 14:47 UTC
    What do you mean "I am removing the duplicate lines"? Your test case clearly contains duplicate lines which are not removed.
    Bill

      At a guess, this is a language barrier issue. He is removing the "duplicateness" by adding a tag which makes each line unique; in a sense, that is "removing duplicates" -- though this isn't the phrase a native English speaker would likely use to describe the process. Gotta' make room for the human factor here; some tolerance in language parsing can be healthy.

        Thanks for the correction. I certainly did not intend any insult, I really did not understand the question.
        Bill