Search code examples
bashsedcommand-line-interface

sed to change string in a file only once


suggest I have a file example as follows:

c
a
b
a
b
d

and I want to change the first occurance of a to e. Then I do this:

sed -i 's/a/e/' example

and all a changed to e.

So is there any way to make sed only replace once within a file?

Thanks.


Solution

  • Applying the information from Aziz' duplicate link to your question, I think this will give you the desired result for your case:

    sed -i '0,/a/s//e/' example