Let's assume that we have the following sample file:
Hello, sed is a powerful editing tool. I love working with sed
If you master sed, you will be a professional one
Let's try to use sed against this file:
$ sed 's/sed/Linux sed/' myfile
Here, we use sed to replace the word sed with Linux sed:
If you check the result carefully, you will notice that sed modified the first word of each line only.
This may not be what you want if you want to replace all occurrences.
Here comes the g flag.
Let's use it and see the results again:
$ sed 's/sed/Linux sed/g' myfile
Now all occurrences are modified.
You can port these modifications to a file using the w flag:
$ sed 's/sed/Linux sed/w outputfile' myfile
Also, you can limit the number of occurrences from the same line, so we can modify the first two occurrences from each line only like this:
$ sed 's/sed/Linux sed/2' myfile
So, if there is a third occurrence, it will be neglected.