Finding & Comparing

Delete or copy every line containing a word on a Mac

You can do this with Note My. Mark every line that mentions a word, then delete the whole set at once — or keep that set and throw the rest away.

Filtering a text file by line sounds trivial until you try it by hand. You want every line containing DEBUG out of a log. You want every line holding a customer reference pulled into a separate note. You want the 200 lines that mention one function, out of the 40,000 that do not. Searching finds them; searching does not remove them, and clicking through 200 matches deleting each line is not a plan.

In Note My the move is two steps: mark every line that matches, then act on the marked set. Marking is a search. Acting is one menu item.

Matching a word is not the same as matching a line

This is the distinction that makes the whole thing work. A find-and-replace with an empty replacement removes the matched text — you get 200 lines with a hole punched in each. What you usually want is the whole line gone, newline included.

Bookmarks bridge that gap. Bookmarking a match marks the whole line it sits on, so once the matches are marked you are working in lines rather than in matches. Two lines that each contain the word four times still count as two lines, and you get two dots in the gutter rather than eight. From there, the Search menu has commands that copy or delete every bookmarked line in the document, in one move and with one undo behind it.

Note

All of this happens in the open document, on your Mac. Nothing is uploaded, there is no account to sign into, and a log you filter never leaves the machine.

Mark every matching line, then act on it

  1. Click the magnifying-glass button in the toolbar and the pop-over opens over your document. ⌘F opens the system find bar, a separate control with its own behaviour.

    Type the word into the find field. Matches highlight live as you type and a running count appears beside the fields. Leave the scope segment on This File: bookmarks live in one document, so marking is a per-document job.

    Set Case Sensitive and Whole Word now, not later. Whole Word is what stops id matching identity, and it is the difference between deleting 200 lines and deleting 900. The find field keeps your recent searches in its menu, so a query you sharpened yesterday is one click away today.

  2. Mark every match

    Press the Bookmark All button — it sits on its own row in the pop-over, under the Find Next, Find Previous and Find All row. The same command is in the menu bar as Search ▸ Bookmark All Search Matches, ⌥⌘B.

    Every line holding a match gets a bookmark dot in the gutter beside its line number. Nothing has changed in the document yet; a bookmark is a marker, not an edit.

  3. Look at what you marked

    Do this before you delete, every time. F2 steps to the next bookmark and ⇧F2 to the previous one, so you can walk the marked set in a few seconds and see whether the pattern caught something it should not have.

    The status bar's Lines counter is the other half of the check: note the number before and after, and you know exactly how many lines went.

    Six counters, live: characters, words, spaces, lines, the size of the selection, and how many times the selected word occurs. They are cached on the document, so a keystroke does not recount two million characters.
  4. Copy the marked lines out, or delete them

    Search ▸ Copy Bookmarked Lines puts every bookmarked line on the clipboard and leaves the document untouched. This is the one to use when you want the matching lines extracted — paste them into a new tab with ⌘N and ⌘V and you have your filtered file.

    Search ▸ Delete Bookmarked Lines removes them. A confirmation appears first — that guard is on by default and lives in Settings ▸ Bookmarks if you ever want it off.

    Deleting changes the open document, not the file on disk. The tab title picks up a leading *, ⌘Z reverses it, and nothing is written to disk until you press ⌘S. If you are unsure of the set, copy the marked lines out first, so you still hold what you removed.

  5. Clear the marks

    Search ▸ Clear removes the bookmarks so the next filter starts empty. ⌘F2 toggles a bookmark on the current line, and right-clicking the gutter offers Add Bookmark to Line N or Remove Bookmark from Line N — useful for rescuing one line from a set you otherwise want gone.

Keeping the matching lines and deleting the rest

The inverse job — throw away everything that does not mention the word — has a safe route and a quick one, and the safe route is the one to reach for. Bookmark the matches as above, run Search ▸ Copy Bookmarked Lines, open a new tab with ⌘N and paste. You now have a file of only the lines you wanted, and the original is untouched. Extracting what you want is always less risky than deleting what you do not.

The quick route uses Search ▸ Invert Bookmarks With Matches, one of the Search menu's bookmark commands, which flips the marked set against the current search matches so the lines you want gone are the ones carrying dots. It is the fastest way to turn "keep these" into "delete the rest" without leaving the document. Run it while the search is still live, then step the marks with F2 to see the new set before you act — the same look-before-you-delete habit that makes the rest of this page safe. ⌘Z steps back at any point.

Matching a pattern instead of a literal word

Tick Regular Expression in the pop-over and the find field takes a pattern. The syntax is the standard one, so a pattern you already use elsewhere behaves the way you expect, and in the replace field $1 stands for the first capture group. Our find and replace keeps the count live while you type, so you can watch the number settle before you mark anything — a useful way to try a pattern out with nothing at stake, since counting changes nothing in the document.

Example patterns for selecting whole lines to bookmark
GoalPattern
Lines mentioning either levelDEBUG|TRACE
Lines that start with a date^\d{4}-\d{2}-\d{2}
Lines holding an IPv4 address\b(?:\d{1,3}\.){3}\d{1,3}\b
Blank lines^\s*$
Lines with a trailing comment//.*$

A pattern only has to match somewhere in the line for that line to be bookmarked, so you rarely need to wrap it in ^ and .*. If the pattern is invalid the pop-over says "Invalid pattern: …" and beeps.

Position is as easy to describe as a word, which is what makes this work on separated data as well as prose. In a comma-separated file, ^(?:[^,]*,){2}[^,]*ERROR marks only the lines whose third field mentions ERROR, and ^(?:[^,]*,){4}\s*$ marks the rows whose fifth field was left empty. Bookmark, step the dots with F2 to confirm you have the rows you meant, then copy them out or delete them exactly as you would for a plain word.

Important

On a very large document, let the status bar's Lines counter be the figure you trust: note it before you delete and again after, and the difference is exactly how many lines went. Tightening the query first — Whole Word on, or a pattern anchored with ^ — keeps the running count close to the set you actually mean to mark.

Counting the lines before you commit

Search ▸ Count Matches reports "N matches on M lines." in a heads-up display. The second number is the one that matters here: it is how many lines Bookmark All would mark, and so how many Delete Bookmarked Lines would remove. It reports on the search you have just run, so reach for it from the Search menu while that search is still live. Think of it as a dry run of the delete: you see the size of the set before you commit to anything, and the document is exactly as you left it.

If the line figure is far from what you expected, tighten the query with Whole Word or anchor the pattern, and count again.

Finding the files worth opening

When the word you are hunting could be in any of forty files, start by seeing where it actually lives. Search ▸ Find in Folder… (⇧⌘F) reads every text file under a folder you choose and lists each file and each matching line, with the match shown in context. Treat it as a survey: you learn the shape of the job — three files with two hits each, one file with eight hundred — before you have changed a character anywhere.

Double-click any result and that file opens in a tab at that line, the cursor already sitting on the match. From there the move is the one you now know: Bookmark All, walk the dots, then copy or delete the marked lines. Working through a folder search this way means every file you open is a file you already know has something in it, and the ones that never matched stay closed and exactly as they were.

Doing it across more than one file

The filter runs in the document in front of you, and the pop-over is built for repeating it. Switch the scope segment to All Tabs and the running count covers every open document at once, so you can tell whether the word appears anywhere beyond the file you are looking at before you go tab by tab. Set the scope back to This File when you are ready to mark, because bookmarks belong to one document.

From the second tab onwards the whole job is three moves, and one of them is a click: the find field keeps your recent searches in its menu, so you pick yesterday's sharpened query rather than retyping it, press ⌥⌘B to mark, then choose Copy or Delete Bookmarked Lines. Ten tabs go by in about a minute, and each one gets the same look at the gutter dots before you act.

The hand-work that surrounds a filter — the tidy-up once the lines are gone, the header you add to every extracted file — is worth recording. Our macro recorder covers bookmark toggling, next and previous bookmark, typing and deletion, movement and selection, paste, find next and previous, single replace, Go to Line and save. Start with Macro ▸ Start Recording (⌥⌘R), do the pass by hand, and stop with Macro ▸ Stop Recording (⌥⇧⌘R); a glass pill counts the actions as they land, and where a step wants a different route you hear about it at record time rather than at playback.

Play it back with ⌥⌘P. The playback scope chooser offers Current Tab or All Open Tabs, so one run can sweep every file you have open. A whole playback undoes in one step: ⌘Z reverses the entire run rather than the last edit inside it. Try it on one tab first, then let it loose on the rest.

Troubleshooting

Bookmark All marked far more lines than I expected

Almost always Whole Word being off. Without it, err matches inside error, errand and terrace. Turn it on, run Bookmark All again, and clear the old marks first with Search ▸ Clear so you are not looking at two sets of dots at once.

Nothing was marked at all

Check the scope segment. If it is on All Tabs your eye may be on a different document from the one that matched. Check case sensitivity too — a tick you left on from an earlier search is the usual culprit. With no matches the pop-over reads "No matches" and beeps on a step or a replace.

The highlights vanished before I could act on them

Closing the pop-over wipes the match highlighting. That is the highlighting only — the bookmark dots in the gutter survive it, and Copy Bookmarked Lines and Delete Bookmarked Lines work from the bookmarks. Two commands do need a live search: Count Matches and Invert Bookmarks With Matches. Run either while the pop-over is still open and the matches are still lit.

I deleted the wrong set of lines

⌘Z. The deletion is an ordinary document edit and nothing was written to disk unless you saved. If you have already saved, press ⌘Z until the lines return, then save again. Our auto-recovery safety net holds recent versions on your Mac too, but treat undo as the route back.

The lines are gone but the blank gaps are not

If you removed the matched text with Replace All rather than removing the lines, you are left with empty lines where the content used to be. Bookmark them with the regular-expression pattern ^\s*$ and delete the bookmarked lines.

A filter you can trust twice

The reason this routine holds up is that every step before the last one is reversible on sight. A search changes nothing. A count changes nothing. A bookmark is a dot in the gutter, not an edit, so you can mark a set, sleep on it, and come back to the same dots. Only the delete touches your text, and it asks before it does; ⌘Z takes it back in one press. Nothing reaches the file on disk until you save.

That is worth more than speed on the day you are filtering something you cannot re-create. Copy the marked lines into a new tab before you delete and you hold both halves of the file: the lines you wanted and the ones you removed. It all happens in the open document, on your Mac — no account, nothing uploaded — so a log of customer references or a private note stays exactly where you put it.

Frequently asked questions

How do I delete every line containing a word?

Search for the word in the Find and Replace pop-over, press Bookmark All (or ⌥⌘B), then choose Search ▸ Delete Bookmarked Lines and confirm. The whole line goes, not just the matched word.

Can I pull the matching lines out into a new file instead?

Yes. Bookmark the matches, then Search ▸ Copy Bookmarked Lines. The document is not modified. Open a new tab with ⌘N and paste.

Does this work on a very large log?

Yes. Our large-file engine keeps a big log responsive, so the bookmark, copy and delete commands behave as they do on a small file. On a file that size the status bar's Lines counter is the cleanest confirmation you can get: read it before the delete and again after, and the difference is your answer. The whole job runs on your Mac, with no account and no upload, so a log full of customer data stays where it is.

How do I open Find and Replace?

Click the magnifying-glass button in the toolbar and the pop-over appears over your document. ⌘F opens the system find bar, a separate control with its own behaviour. Once you are in the pop-over the commands you repeat have their own keys: ⌥⌘B for Bookmark All Search Matches, F2 and ⇧F2 to step the marks.

How do I filter a whole folder of files?

Start with Search ▸ Find in Folder… (⇧⌘F), which lists every file and line under a folder that matches. Double-click the results worth acting on and each file opens in a tab at the matching line. Then run the same two steps in each tab — Bookmark All, then Copy or Delete Bookmarked Lines. The find field holds your query in its recent searches, so every tab after the first is a click and two commands.

How do I keep only the lines that match?

The reliable way is to bookmark the matches and use Search ▸ Copy Bookmarked Lines into a new tab — nothing is deleted, so nothing can go wrong. Search ▸ Invert Bookmarks With Matches inverts the marked set against the current matches if you would rather delete in place; step the gutter dots with F2 and check them before you do.