![Forrest logo](/images/logo.png)
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 tellssed
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
.
- The
'/${string}/d'
: This part is thesed
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 insed
are used to enclose patterns. d
is ased
command that tells it to delete lines matching the pattern.
- The
${file}
: This is the variable representing the file name or the actual file path. It specifies the file on which thesed
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.