Keep in mind that it’s a good idea to test your changes to files before you make them permanent, especially if you’re just getting used to the command. You can omit the “-i” and, if the output looks like what you expected, run the same command with the -I to proceed with the changes to the file.
2. Global replacements
To replace every instance of a certain word in a file, you would need to use a command like the one shown below where the “g” means “global”. The command will change every instance of “Mar” to “Apr” in the schedule file. Again, because it includes the -i (lowercase “i”) option, the file is changed in place.
$ sed -i 's/Mar/Apr/g' schedule
If Mar might be spelled as “Mar” or “MAR”, you can add the case-insensitive option (g) in a command like this:
$ sed -i 's/Mar/Apr/Ig' schedule
Note that it’s the “I” (capital i) before the “g” that makes the command case-insensitive.
3. Specific line changes
To change only one specific line in a file, use a command like the one below that includes the line number (i.e., 2) to ensure that the change is only made in the specified line.
$ sed -i '2s/Mar/Apr/' schedule
4. Deleting lines
You can also use sed to delete lines in a text file. The command below would delete line 3 in the file.