I'm trying to replace the first occurrence in a file using sed
:
sed -i s/he/He/ dummy.txt
Instead of replacing first occurrence, it replaces all the occurrences, even without /g
.
According to the documentation it should replace the first only.
The sed
version which is:
GNU sed version 4.1.5
Am I missing anything? Or does the behavior differ for different sed
implementations?
sed
is basically a line editor - it works on one line at a time (unless you program it to read more lines into its buffer (called the pattern space). Where you have seen "g
will replace the first occurrence only" refers to the first occurrence in the current pattern space, ie. in the current line. – Peter.O Jun 19 '15 at 09:51