Reputation: 45
Let's say I am having file a.txt which is having following content.
aa
aa
bb
cc
dd
ee
ff
ee
gg
I want following output -
aa
aa
bb
cc
dd
ee
ff
gg
Note that I want delete particular duplication of lines only: ee
.
How can I do that following one liner I tried.
perl -ne 'print unless $a{$_}++' a.txt
but it is deleting all duplicated lines.
Upvotes: 2
Views: 69
Reputation: 66964
To remove duplicates of only specific ("target") lines add that condition
perl -lne'$tgt = "ee"; print unless $h{$_}++ and $_ eq $tgt' file
If there may be multiple such targets, then check whether the current line matches any one of them. A nice tool for that is any
from List::Util
perl -MList::Util=any -lne'
$line=$_;
@tgt = qw(ee aa);
print unless $h{$line}++ and any { $_ eq $line} @tgt
' file
Target(s) can be read from the command line as arguments, if there is a benefit to not have them hardcoded.
Note: In older versions any
is in List::MoreUtils, and not in List::Util
.
Upvotes: 3