Caxton
Features Benchmarks Pricing Docs How it works Changelog Support About Download Free
Guides
All guides Large File Text Editors EmEditor for Mac UltraEdit Alternatives Notepad++ Alternatives Notepad for Mac File Too Large for VS Code CSV Editor for Mac Open a CSV File CSV Viewer for Mac Edit CSV Without Auto-Formatting Repair a Malformed CSV Remove Duplicate Rows Validate a CSV Transform a CSV Column Fixed-Width to CSV Sort a Large CSV CSV Too Big for Excel Open a Huge CSV Log Viewer for Mac View Log Files Open a 10 GB Log Filter Large Log Files Jump to a Time in a Log Find & Replace in Huge Files Find & Replace Across Files Regex Cheat Sheet Compare Two Text Files Open a SQL Dump Fix Garbled Text & Encoding Convert Line Endings Sort & Dedupe Lines Open a GBK or Big5 File Open a Large JSON File Numbers File Too Large

Sort lines and remove duplicate lines on a Mac.

A list of email addresses with repeats, a log of hostnames, an export that was pasted together twice: the job is to put the lines in order, or to keep one of each, or both. TextEdit cannot sort lines, so the question comes up often. Every Mac already has the commands that do it, and the one choice that matters is whether the result keeps the original order. This guide shows the free commands first and then the menu commands in Caxton.

Download Caxton for Free · 7-day free trial of Caxton Pro, no credit card required · 7 MB · macOS 13.0+

The short answer

On any Mac, sort puts a file's lines in order and sort -u keeps one copy of each, while awk '!seen[$0]++' removes duplicates and keeps the lines in their original order, all free in Terminal. Caxton, a native Mac editor for text and data files, does the same from the Text menu: Sort Lines and its numeric, descending and by-length forms, and Delete Duplicate Lines, which keeps the first copy of each line in place, each as one undo step on a selection or the whole document.

How do I sort the lines of a text file in Terminal?

sort is the tool, and it ships with macOS. It reads the file, orders the lines, and prints the result, so send it to a new file rather than overwriting the one you are reading:

sort names.txt > names-sorted.txt

By default the order is character by character, so uppercase letters sort before lowercase and "10" sorts before "9". Two options change that. -f folds case so that "apple" and "Apple" sort together, and -n sorts by numeric value, which Apple's manual page describes as "Sort fields numerically by arithmetic value." -r reverses the order.

sort -f names.txt
sort -n sizes.txt
sort -rn sizes.txt

sort is also the right tool for a file larger than memory. Apple's manual page says that when the input does not fit in its buffer, "the temporary disk files are used to perform the sorting," with -T choosing the directory for them, so a multi-gigabyte file sorts, slowly, without anything else to install.

How do I remove duplicate lines in Terminal?

There are three commands, and they differ in one thing: the order of the result.

sort -u sorts and keeps one copy of each line. The manual page calls it "Unique keys. Suppress all lines that have a key that is equal to an already processed one." The result is sorted, which is often fine and sometimes not.

sort -u hosts.txt > hosts-unique.txt

uniq removes repeated lines, but only when they sit next to each other. Its manual page is explicit: it works by "comparing adjacent lines," and "Repeated lines in the input will not be detected if they are not adjacent, so it may be necessary to sort the files first." That is why uniq is nearly always written after sort:

sort hosts.txt | uniq

Two of its options answer other questions. uniq -c counts, printing each line with the number of times it appeared, and uniq -d prints only the lines that were repeated:

sort hosts.txt | uniq -c | sort -rn
sort hosts.txt | uniq -d

awk '!seen[$0]++' removes duplicates and keeps the original order. It prints a line the first time it sees it and never again, because the counter for that line is zero only once:

awk '!seen[$0]++' hosts.txt > hosts-unique.txt

Run on a file of b, a, b, c, a, it prints b, a, c. It holds every distinct line in memory, so it fits a file of any length whose distinct lines fit in memory. All three commands compare lines exactly: a trailing space or a different capitalization makes two lines different, and a sort with -f does not make the dedupe ignore case.

How do I sort lines and remove duplicates in Caxton?

Caxton, a native Mac editor for text and data files, puts the same operations in the Text menu, where they act on the selected lines or, with nothing selected, on the whole document. Each one lands as a single undo step, and the status bar reports what changed. Here is the walk through a file of hostnames.

1. Open the file and select the lines, or nothing

Drag the file onto Caxton or use File ▸ Open. With no selection a Text command works on every line; select a range to work on only those. Opening, reading and searching are Caxton Free.

2. Sort

Text ▸ Lines holds five sorts. Sort Lines orders A to Z, ignoring case, and lines that compare equal keep their order, so a sort followed by a dedupe gives the same result every time. Sort Lines Descending is Z to A. Sort Lines Numerically sorts by the number each line starts with, comparing values rather than digits, so 9 comes before 10, and lines that do not start with a number go last in their original order. Sort Lines by Length puts the shortest line first, and Reverse Lines puts the last line first. Changing the file is Caxton Pro, which every download includes as a 7-day free trial with no credit card required.

3. Delete the duplicates

Text ▸ Lines ▸ Delete Duplicate Lines keeps the first copy of each line and removes the later ones, so the file keeps its original order, the same result as the awk route above. Lines must match exactly, case included. The opposite question has its own command: Keep Only Duplicate Lines keeps every occurrence of the lines that appear more than once and drops the rest, which is the quick way to see what was repeated. Both are Caxton Pro.

4. Check, then undo or save

The status bar reports the count, and ⌘Z reverses the whole operation as one step. ⌘S writes the file back. To see which lines repeat without changing anything, use the free route first: Edit ▸ Find ▸ Filter Lines (⇧⌘L) with a hostname shows every line that contains it, with the count in the status bar.

5. The size limit, and what to do past it

Text menu commands work on up to 256 MB at a time. On a larger document the command says so and asks for a smaller selection, so select a region of the file and run it there, or sort the file once with sort in Terminal and open the result. A file that is really a table has a better route, described next.

Duplicate lines or duplicate rows?

A line is a duplicate when every character matches. A row in a CSV is usually a duplicate when a few columns match, such as the same email address with a different signup date, and the line-based tools cannot see that. For a table, Caxton opens the file in its grid and offers CSV ▸ Sort Rows… by one or more columns and CSV ▸ Delete Duplicate Rows… by a key you choose, both streamed, with the duplicate groups reported before anything is removed. Remove duplicate rows from a large CSV walks through it, and sort a large CSV covers the ordering.

Which approach fits which job?

JobFree in TerminalIn Caxton
Sort a listsort, with -f, -n or -rText ▸ Lines ▸ Sort Lines and its variants
Remove duplicates, order not importantsort -uSort Lines, then Delete Duplicate Lines
Remove duplicates, keep the original orderawk '!seen[$0]++'Delete Duplicate Lines
Count how often each line appearssort | uniq -cKeep Only Duplicate Lines, or Filter Lines for one value
A file larger than memorysort, which spills to diskSelect a region under 256 MB, or sort it in Terminal first
Duplicate rows by a columnsort -t, -k with uniq on a fieldCSV ▸ Delete Duplicate Rows…

Open the file, pick Text ▸ Lines ▸ Delete Duplicate Lines, and read the count in the status bar before you decide to keep it.

Download Caxton for Free

7-day free trial of Caxton Pro, every feature, no credit card required. After it, Caxton Free keeps opening, searching and filtering at any size.

Frequently asked questions

How do I remove duplicates but keep the original order?

In Terminal, awk '!seen[$0]++' file.txt prints each line the first time it appears and skips every later copy, so the order stays as it was. In Caxton, Text ▸ Lines ▸ Delete Duplicate Lines does the same: it keeps the first copy of each line in place and removes the later ones, as one undo step.

How do I sort lines numerically instead of alphabetically?

sort -n compares the numbers by value, so 9 comes before 10, where a plain sort puts 10 first because it compares character by character. In Caxton, Text ▸ Lines ▸ Sort Lines Numerically sorts by the number each line starts with, smallest first, and puts lines that do not start with a number last in their original order.

How do I find which lines are duplicated without deleting them?

sort file.txt | uniq -d prints only the lines that appear more than once, and uniq -c prints every line with its count. In Caxton, Text ▸ Lines ▸ Keep Only Duplicate Lines keeps every occurrence of the repeated lines and drops the rest; undo brings the file back, and Filter Lines shows every line that contains one value without changing anything.

Can I sort a file that is several gigabytes?

Yes, with sort in Terminal: Apple's manual page says it uses temporary disk files when the input does not fit in its buffer, so a multi-gigabyte file sorts without anything else to install. Caxton's Text menu commands work on up to 256 MB at a time, so on a larger file select a region or sort it in Terminal first. A large CSV sorts as a table through CSV ▸ Sort Rows, which streams.

Sources