Global replacement

Let's assume that we have the following sample file:

Hello, sed is a powerful editing tool. I love working with sedIf 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 ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.