Here's the fastest and shortest way I could find to process an input file, and print the unique lines inside it:
perl -ne "$s{$_} ||= print" < in > out
Explanation: The ||= operator has a behaviour called "short-circuit". If the left-hand side of the operation evaluates to true, the operation will short-circuit and stop - essentially the right-hand side will be ignored and never evaluated. But if the left side is false, then whatever the right-hand side evaluates to will be assigned to the left.
In this case, we are processing each line from a file called "in". The first time we see $s{$_} it will evaluate to false, setting off the right hand side to be evaluted. This will print $_, return a 1, then set $s{$_} to 1.
The next time we see $s{$_} inside the loop, it is true, so the ||= never again runs the right-hand side operation. This results in the above code only printing a line the first time it's seen, and no more.
| [reply] [Watch: Dir/Any] [d/l] [select] |
perl -ne '!$s{$_}++ && print' < in > out
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] [Watch: Dir/Any] [d/l] |
That executes more slowly. You have to waste time
creating new variables because of using postincrement
rather than preincrement.
| [reply] [Watch: Dir/Any] |