Forrest logo
back to the sed tool

sed:tldr:33c7c

sed: Replace all `apple` (basic regex) occurrences with `mango` (basic regex) in a `file` and save a backup of the original to `file.bak`.
$ sed -i bak 's/apple/mango/g' ${filename}
try on your machine

This command uses the sed command-line tool to perform a search and replace operation on the contents of a file.

Here's a breakdown of the command:

  • sed: The command-line tool for stream editing.
  • -i bak: The -i flag specifies that the changes should be made in-place, modifying the file directly, and the bak argument specifies that a backup copy of the original file should be created with a ".bak" extension.
  • 's/apple/mango/g': This is the sed expression used for search and replace. The s denotes a substitution command. The expression apple is the pattern to be searched, and mango is the string that will replace each occurrence of the pattern. The g at the end stands for global, meaning it will replace all occurrences of the pattern in each line.
  • ${filename}: This is a variable containing the name of the file on which the command will be applied. The actual filename must be provided when executing the command.

In summary, the command replaces all occurrences of the string "apple" with "mango" in the contents of the specified file, modifying the file in-place and creating a backup copy with a ".bak" extension.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the sed tool