I'm trying to delete matching patterns, starting from the second occurrence, using sed
or awk
. The input file contains the information below:
abc
def
abc
ghi
jkl
abc
xyz
abc
I want to the delete the pattern abc
from the second instance. The output should be as below:
abc
def
ghi
jkl
xyz
One way using
awk
:Just set
p
to pattern that you only want the first instance of printing:Taken from : unix.com
Using
awk '!x[$0]++'
will remove duplicate lines. x is a array and it's initialized to 0.the index of x is $0,if $0 is first time meet,then plus 1 to the value of x[$0],x[$0] now is 1.As ++ here is "suffix ++",0 is returned and then be added.So !x[$0] is true,the $0 is printed by default.if $0 appears more than once,! x[$0] will be false so won't print $0.Just change the "2" to "3" or whatever number you want to keep the first N occurrences instead of just the first 1.
Neat
sed
solution: