1

I need to change the 3rd occurrence of 'None' in a file text.txt using the sed command. I found this command

sed 's/None/All/3' text.txt

but this works for 3rd occurrence of 'None' in a line, not in the entire document. Is there any way to use sed to look for 'None' in the entire document and change only the 3rd occurrence?

I could use the line number, but if my document changes then the command won't work.

1 Answer 1

4

Given GNU sed, you can kind of fake it by adding the -z (or --null-data) option:

-z, --null-data
             separate lines by NUL characters

Assuming your document doesn't actually contain null-delimited data, this will cause sed to treat the whole document as a single line so that the 3 modifier assumes the meaning that you want

sed -z 's/None/All/3' text.txt
1
  • Thank you nam. it worked!!!, thank you, thank you... you are the best.
    – smaqsood
    Nov 29, 2019 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .