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.
In reply to Re: To remove duplicate lines and add digits as per loop
by Fletch
in thread To remove duplicate lines and add digits as per loop
by suvendra123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |