Filter a log or list by deleting what you do not need

Delete Lines Containing Text on Mac

Deleting every line that contains a word is a one-line regex, but it is easy to get the last line, the line break, or the capitalization wrong. Here are patterns that were tested, plus Terminal equivalents and a safer non-destructive route.

notePad++++ search and replace with regex, showing match counts per file and replacement previews

The idea

To delete every line that contains a word, you match the entire line and replace it with nothing. In regular expressions, ^.*word.* matches a whole line containing word, and adding \n? at the end includes the line break so that no empty line is left behind.

Option 1: Regex find and replace in notePad++++

  1. Open the file and open the search panel.
  2. Turn on Regex. Turn on Match Case only if capitals matter.
  3. In the find field, enter the pattern below.
  4. Leave Replace with empty.
  5. Choose Replace All. The app reports how many replacements were made.

Patterns to copy, using error as the example word:

You want to Pattern
Delete lines containing error ^.*error.*\n?
Delete lines that do not contain error ^(?!.*error).*\n?
Delete lines that start with # ^#.*\n?
Delete blank lines ^[ \t]*\n
Delete lines containing foo or bar `^.*(foo

These were run against sample text with a regex engine using the same rules as the editor: a line-anchored pattern with case-insensitive matching. On a small test, ^.*error.*\n? removed every line containing error, in any capitalization, and left the other lines intact.

A safer route: keep, do not delete

If you are not sure you want to lose the lines, extract instead. Extract to New Tab copies every line that matches your search into a new tab and leaves the original file as it was. You can review the result and only then decide what to do.

Option 2: Terminal

To write the result to a new file without touching the original:

grep -v 'error' log.txt > cleaned.txt

grep -v prints every line that does not contain the text, which is the same as deleting the lines that do. Matching is case sensitive. Add -i to ignore capitals.

To edit the file in place, macOS needs an empty string after -i:

sed -i '' '/error/d' log.txt

This is where most Mac users get tripped up: the Linux form sed -i '/error/d' fails on macOS because its sed is the BSD version. The command above was tested on macOS and deletes only the lines that match exactly as typed, so it left a line containing ERROR in place.

In-place editing overwrites the file. Keep a copy.

Choosing between them

Situation Best route
A one-off cleanup where you want to see it happen Regex Replace All in notePad++++
You are unsure and want to review first Extract to New Tab
A script or a repeated job on many files grep -v or sed in Terminal
Capitalization must be ignored Regex with Match Case off, or grep -vi

Common mistakes

  • Forgetting the line break. Without \n?, the text disappears but an empty line remains.
  • Matching too much. .* is greedy within a line. A pattern such as ^.*id.* also matches video and provide. Anchor the word if that matters, for example ^.*\bid\b.*\n?.
  • Running it on the only copy. Save first, or work on a duplicate.

FAQ

Is the search case sensitive?

In notePad++++ it ignores capitals unless you turn on Match Case. In the Terminal sed and grep examples below, matching is case sensitive by default, so ERROR and error are different.

How do I keep only the lines that contain the word?

Use Extract to New Tab, which copies the matching lines into a new tab and leaves the original untouched. In Terminal, grep 'word' file prints only the matching lines.

Why does my regex leave an empty line behind?

The line break at the end of each line is part of what you need to match. End the pattern with \n? so the break is deleted along with the text.

Can I undo a Replace All?

Work on a copy of the file first, or save before you start. After Replace All the app reports how many replacements were made, which is a quick check that the count matches what you expected.

Notepad++ is a trademark of its respective owner. notePad++++ is an independent macOS text editor and is not affiliated with, endorsed by, or approved by the official Notepad++ project.