Using awk
, counting how many lines contain the string Copyright
, and when we reach the third such line, perform a substitution of the first such occurrence on that line:
awk '/Copyright/ && ++n == 3 { sub("Copyright", "No-&") } { print }' file
Using &
in the replacement means using whatever the regular expression matched. In this case it will be the string Copyright
that will be inserted. This (using &
) also works in e.g. sed
.
The condition before the first block,
/Copyright/ && ++n == 3
would match the string Copyright
against the current line. If it matches, n
would be incremented. If the incremented value of n
is equal to 3, the block is executed.
Testing:
$ cat file
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C
(the file is the same as yours, but with the contents duplicated once)
$ awk '/Copyright/ && ++n == 3 { sub("Copyright", "No-&") } { print }' file
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# No-Copyright (C) 2015-2016 C
# Golden dictionary is a versatile multi purpose reference
# Copyright (C) 2004-2008 A
# Copyright (C) 2008-2015 B
# Copyright (C) 2015-2016 C
No-
? Or the third occurrence no matter where it is?