Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I sort lines in a text file without changing the original?

Quick answer

On systems with GNU Coreutils, run sort filename.txt to display sorted lines in the terminal; by default, GNU sort sends results to standard output rather than replacing the input. To keep a sorted copy, redirect output to a different filename. Check your system’s sort behavior and locale first, because ordering can vary and options can change it. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a text file and want to see its lines alphabetically, but I don’t want to risk changing it. What’s the safest first step?

Geez

First, preview the result without redirecting it into a file. On a system with GNU Coreutils, open a terminal in the file’s folder and run:

sort notes.txt

Replace notes.txt with your file’s name. GNU sort reads the named file and, by default, writes its sorted output to standard output rather than replacing the input file. [S1] That means the command displays a result; it does not, by itself, save that result over the original. This behavior is documented for GNU sort, not every system’s utility, so check which sort your system provides before relying on it.

Follow-up question

What would I see, and how can I check that the preview is useful before saving anything?

Geez

Suppose notes.txt contains these lines:

pear
Apple
banana

Running sort notes.txt prints an ordered version in the terminal, while the file remains your input. Before saving, compare the displayed lines with what you expect: are all the lines present, and does the order look right for your task? GNU sort compares whole lines by default, and the active locale’s character-collating sequence can affect ordering. [S1] So don’t assume that capitalization, punctuation, accents, or other characters will sort exactly as you expect; verify the preview on your own system.

Follow-up question

I’m happy with the preview. How do I keep the sorted result while preserving the unsorted file?

Geez

Write the output to a different filename, for example:

sort notes.txt > notes-sorted.txt

Here, notes.txt is the input and notes-sorted.txt is the separate output. Check that the output name is not the same as the input name: redirecting output to the original path is not a safe way to preserve it. If notes-sorted.txt already exists, choose a new name or decide deliberately whether to replace that separate file. Then inspect the saved copy and, if needed, compare it with the original. This example assumes a shell and GNU sort; other tools may differ.

Follow-up question

What if the order isn’t what I expected? Can I change the sort without accidentally changing the source?

Geez

Yes. You can try a different documented option on a preview, but check what that option means for your particular sort implementation before using it. GNU sort supports options that change keys or ordering, and locale also affects comparison. [S1] The right setting depends on your language, file format, and goal; the behavior described here does not cover every platform’s command. Keep the input path unchanged, preview each variation without redirection, and save only a result you have checked under a different filename. If you need a specialized order, consult your system’s utility documentation.

A practical checklist

  • Confirm which sort utility your system provides; the behavior described here is GNU-specific.

  • Run sort input.txt without redirection to preview the result.

  • Check that all lines are present and the displayed ordering meets your expectations.

  • If needed, verify how your system’s locale and chosen options affect ordering.

  • Save only to a distinct output filename, then inspect that copy.

A few more useful answers

Does `sort input.txt` overwrite the input file?

For GNU sort, the default output goes to standard output, not back into the named input file. [S1] Don’t assume every platform’s sort behaves identically.

Why does the order differ from what I expected?

GNU sort uses the active locale’s character-collating sequence, and options can change which keys or ordering are used. [S1] Check the preview and your system’s documentation rather than assuming a universal order.

Can I make a sorted copy without changing the original?

On a shell using GNU sort, redirect the output to a different filename, such as sort input.txt > sorted-copy.txt. Make sure the output path is not the input path.

Sources

References used for this guide.

  1. [S1] GNU Coreutils 9.11: sort text files
    gnu.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work