How to replace the first occurrence of a string in a file using sed

Finding a solution for this was bordering on ridiculous, no matter how I put it… “replace using non greedy”, “replace only one instance of a string”, “replace first string match only” I struggled to find an answer. The man page had no information on this either.
After a while I came up with the following solution.

sed ‘0,/this/s//to_that/’ filename.txt

After tweeting for some help on how this actually works, a colleague sent me the following link


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s