Welcome to the next pikoTutorial!
Basic usage
At its most basic, the sort command sorts lines in a file alphabetically. For example, if you have a file named data.txt, you can print its sorted content with:
sort data.txtOf course, such console output can be redirected to a file:
sort data.txt > sorted_data.txtNumerical sorting
Instead of performing the default alphabetical sorting, by adding the -n option, you can sort the file content numerically:
sort -n data.txtNote for advanced: maybe you noticed that if you have a file with 3 lines e.g. “3 1 2”, the default sort (without
-noption) will output lines in the correct order “1 2 3”, so what’s the point of adding-noption? The difference shows up when you work with files containing mixed letters and numbers. Let’s say there is a file with lines “1 a 2 b”. The defaultsortwill sort it to alphabetical order “1 2 a b” andsortwith-noption will output “a b 1 2”. Why does the numerical sort puts letters first? The answer lies in howsorttreats non-numeric character – it assumes their value to be 0 and 0 goes in front of both 1 and 2.

AI is powerful. Snippets are instant.
Stop prompting for the same patterns repeatedly. Get almost 100 free VS Code snippets for C++, Python, CMake and Bazel from piko::snippets GitHub repository.
Reverse order
To reverse the order of the sort, call:
sort -r data.txtRemoving duplicates
sort also provides an option for basing filtering the output by removing duplicates:
sort -u data.txtSorting by column
Often there are files which consist of multiple columns:
5 2 7
2 8 5
1 7 4If the columns are separated with white space, we can sort such file not only by the first character, but also by any given column. To sort it by the second column, call:
sort -k 2 data.txtThis will output:
5 2 7
1 7 4
2 8 5If your file contains data separated by some other delimiter, e.g. comma, you must specify this delimiter explicitly with -t option:
sort -t, -k 2 data.txtNote for beginners: remember that not all characters can be used directly in the command line. If your delimiter is e.g. a semicolon, you must provide it as
-t";".
Randomizing lines
As it turns out, sort allows not only for sorting, but also for the opposite – for randomizing the lines in the given file:
sort -R data.txtDon’t confuse it with -r option which stands for “reverse”.
Read also:
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?
- Bug of the week #3
- Build & run C++ unit tests with CMake
- Arrange text with sort on Linux
- Key derivation function with Python
- Let’s review some code #1: C++









