I'm trying to replace the first occurrence in a file using sed:sed -i s/he/He/ dummy.txtInstead 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.5Am I missing anything? Or does the behavior differ for different sed implementations?
Why does sed replace all occurrences instead of only the first occurrence?
sed
sed processes input line by line. The /g modifier influences whether it replaces all the occurrences on a line, or only the first one:echo hehe | sed s/h/H/echo hehe | sed s/h/H/gTo replace only the first occurrence in the file, use something like the following:s/h/H/            # Replace.ta                # Go to a if s/// worked.b                 # Otherwise, start the next cycle.:a { n            # Process the next line.     ba           # Go to a.}