Tag Archives: ed

Text Manipulation: How to Globally Delete Lines Matching a Certain Pattern

In order to delete complete lines that match a certain pattern, you can use various tools. I find that the easiest tools to use in this situation are perl or our humble elderly friend ed.

For example, say we want to delete all lines containing the string “delete_me”, appearing at the very beginning of the line. The following two commands will have the desired effect of deleting the required lines.

Using perl:

Using ed:

Of course, there are many more ways of achieving our goal but these are my two personal favourites. It goes without saying that you can modify the search pattern to meet your exact requirements.