Multiple ways to sort a text file

October 28, 2015 — Leave a comment

There’s a text file (or output from a command) that needs sorting! You can do just about anything in Linux.. in this extremely short tutorial, we’ll show you how to sort a text file. You can also sort the output of a command.

For sorting, we’ll use the sort Linux command.

We have a file, it’s made up of random words.. like:
—–
huzzaing
jumbucks
dizzying
mezuzahs
jipijap
—–

Let’s sort then so they’re in alphabetical order.. we’ll use sort’s ‘d’ (dictionary) flag:

We come up with this:
—–
dizzying
huzzaing
jipijapa
jumbucks
mezuzahs
—–

Let’s say we now want to put them back into random order.. use the -R (random) flag:

and it outputs:
—–
jumbucks
dizzying
huzzaing
mezuzahs
jipijapa
—–

How about numeric? Here’s a file with 5 numbers all mixed up:
—–
43534
667534
21334
45466
57
—–
Use the -n flag:

The output:
—–
57
21334
43534
45466
667534
—–

Oops.. maybe we wanted the lowest number at the bottom.. use -r along with the -n for reverse:

The output:
—–
667534
45466
43534
21334
57
—–

And there you have sort! Read the man page for more options!




No Comments

Be the first to start the conversation.

Leave a Reply