[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;

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: