I'm trying to print duplicate lines from the filehandle, not remove them or anything else I see asked on other questions. I don't have enough experience with perl to be able to quickly do this, so I'm asking here. What's the way to do this?
相关问题
- $ENV{$variable} in perl
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Change first key of multi-dimensional Hash in perl
- How do I get a filehandle from the command line?
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- What is a good way to deploy a Perl application?
- Speeding up Selenium Webdriver
try this
Using the standard Perl shorthands:
As a "one-liner":
More data? This prints
<file name>:<line number>:<line>
:Explanation of
%seen
:%seen
declares a hash. For each unique line in the input (which is coming fromwhile(<>)
in this case)$seen{$_}
will have a scalar slot in the hash named by the the text of the line (this is what$_
is doing in the has{}
braces).x++
) we take the value for our expression, remembering to increment it after the expression. So, if we haven't "seen" the line$seen{$_}
is undefined--but when forced into an numeric "context" like this, it's taken as 0--and false.So, when the
while
begins to run, all lines are "zero" (if it helps you can think of the lines as "not%seen
") then, the first time we see a line,perl
takes the undefined value - which fails theif
- and increments the count at the scalar slot to 1. Thus, it is 1 for any future occurrences at which point it passes theif
condition and it printed.Now as I said above,
%seen
declares a hash, but withstrict
turned off, any variable expression can be created on the spot. So the first time perl sees$seen{$_}
it knows that I'm looking for%seen
, it doesn't have it, so it creates it.An added neat thing about this is that at the end, if you care to use it, you have a count of how many times each line was repeated.
Prints dupes only once:
If you have a Unix-like system, you can use
uniq
:or
should do what you want. More information: man uniq.