Forrest logo
back to the sed tool

file_manipulation:warp:be41aa3050d444ffa85e681aa5962dc5

Delete all lines that contain a specific string from a text file
$ sed -i '' '/${string}/d' ${file}
try on your machine

This command uses the sed command-line tool to delete lines containing a specific string in a file.

Here is a breakdown of the command:

  • sed: The name of the command-line tool used for text manipulation.
  • -i '': This option tells sed to edit the file in-place, meaning it modifies the file directly rather than printing the result to the console.
    • The '' after -i specifies that a backup of the original file should not be created. If you want to create a backup, you can specify a file extension like -i.bak.
  • '/${string}/d': This part is the sed pattern. It is enclosed in single quotes to prevent variable expansion. ${string} is a placeholder for the actual string you want to search for and delete.
    • The /.../ delimiters in sed are used to enclose patterns.
    • d is a sed command that tells it to delete lines matching the pattern.
  • ${file}: This is the variable representing the file name or the actual file path. It specifies the file on which the sed command should be applied.

To use this command, you need to replace ${string} with the actual string you want to delete and ${file} with the file on which you want to apply the command.

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