How to replacecan I replace the third occurrence ofNth (for example, the string third) occurrence
of a string (or pattern) in the filea file using the sed command?
I do mean the Nth occurrence in the file,
not the Nth occurrence in a line
or the first occurrence in the Nth matching line.
There may be multiple occurrences in a line,
and an occurrence (a match) might be a partial word.
Example:
Change only the third occurrence of is to us in the file.
My input file contains:
hai this is linux.
hai this is unix.
hai this is mac.
hai this is unchanged.
The expected output is:
hai this is linux.
hai thus is unix.
hai this usis mac.
hai this is unchanged.
Note that “this” has been replaced by “thus” in the second line.