Skip to main content
Reverted revision #6 and explained why it was wrong.
Source Link

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.

How to replace the third occurrence of the string in the file using sed command.

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 us mac.
hai this is unchanged.

How can I replace the Nth (for example, the third) occurrence of a string (or pattern) in a 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 is mac.
hai this is unchanged.

Note that “this” has been replaced by “thus” in the second line.

input and output files are exactly the same that's obviously not expected
Source Link

How to replace the third occurrence of the string in the file using sed command.

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.

I am expectingThe expected output is:

hai this is linux.
hai thus is unix.
hai this isus mac.
hai this is unchanged.

How to replace the third occurrence of the string in the file using sed command.

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.

I am expecting output is:

hai this is linux.
hai thus is unix.
hai this is mac.
hai this is unchanged.

How to replace the third occurrence of the string in the file using sed command.

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 us mac.
hai this is unchanged.
edited tags; edited title
Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

How to replace only the thirdNth occurrence of the filea pattern in unix using only sed commanda file?

Tweeted twitter.com/#!/StackUnix/status/561080312672043008
edited body; edited tags
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
added 19 characters in body
Source Link
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185
Loading
Source Link
Sureshkumar
  • 131
  • 1
  • 1
  • 3
Loading