[TIP] delete a line in-place with GNU sed

Case:
Suppose you have a file named testfile with the following content:

one
two
three

Say, you want to delete those lines that start with the string tw (that is two in our example). Here is the GNU sed command to use:

Incantation:

sed -i -e "/^tw.*$/d" testfile

Result:
now testfile has this content:

one
three

Notes:
*) -i stands for “in-place”, i.e. perform the command directly in the file (GNU sed only);
*) The caret (^) matches the beginning of the line;
*) A period (.) matches any single character;
*) The asterisk (*) matches zero or more occurrences of the previous character;
*) The dollar sign ($) matches the end of the line;
*) d is the delete command;

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*