How to delete the matching pattern from given occu

2020-04-14 08:13发布

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

4条回答
Ridiculous、
2楼-- · 2020-04-14 08:34

One way using awk:

$ awk 'f&&$0==p{next}$0==p{f=1}1' p="abc" file
abc
def
ghi
jkl
xyz

Just set p to pattern that you only want the first instance of printing:

查看更多
Lonely孤独者°
3楼-- · 2020-04-14 08:37

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.

查看更多
Fickle 薄情
4楼-- · 2020-04-14 08:39
$ awk '$0=="abc"{c[$0]++} c[$0]<2; ' file
abc
def
ghi
jkl
xyz

Just change the "2" to "3" or whatever number you want to keep the first N occurrences instead of just the first 1.

查看更多
Evening l夕情丶
5楼-- · 2020-04-14 08:53

Neat sed solution:

sed '/abc/{2,$d}' test.txt
abc
def
ghi
jkl
xyz
查看更多
登录 后发表回答