Sometimes you may want to sort the result of a command, or contents of a file, or even command line input in Linux. The sort command allows you to do all these three things at one go. It allows you to sort values and columns in ascending, descending or even custom order. Also, it does not modify the original input but only the output. This command is available in almost every linux distribution and part of most shells, not just bash shell.
Bash Sort By Column
Here is the syntax for sort command.
sort [options] input
You may change the sort order using various options. By default, sort command
- Sorts alphabets in ascending order
- Numbers are sorted before alphabets
- Lowercase letters have more precedence than uppercase letters
Here are the most commonly used options available for sort command.
- -n – sorts in numerical values
- -h – compare human-readable numbers such as 1k, 1G
- -R – random sort order but group the identical keys
- -r – reverse sort (descending order).
- -o – save output to file
- -u – show unique values
- -k – sort data by specific key/column
Now we will look at some common use cases to sort values in bash shell.
1. How to Sort Numerical Values in bash
Let us say you have the following file that contains numbers
$ cat data.txt 34343 334343 232323 232 32124 21 576767
Here is the command to sort the data numerically.
$ sort -n data.txt 21 232 32124 34343 232323 334343 576767
2. How to Sort in Reverse Order
If you want to sort the above data in reverse order, just use -r option
$ sort -r data.txt 576767 34343 334343 32124 232323 232 21
Let us say you have the following file with alphanumeric values.
$ cat data1.txt 34eejnj eredsffe 33r ffeff f3f4ff fdgfvv hgjhgh
Here is how to reverse sort the values above.
$ sort -r data1.txt hgjhgh ffeff fdgfvv f3f4ff eredsffe 34eejnj 33r
In the above example, sort command outputs all lines starting with alphabets before those starting with numbers. Among them, it displays lines beginning with alphabets in descending order of alphabets, and then displays all lines beginning with numbers in descending.
3. How to Sort by Column in Bash
Sort command allows you to sort data by column using -k option. Let us say you have the following file.
$ cat data2.txt 3 abc 943 2 def 234 1 xyz 178
If you want to sort the data by 3rd column, you need to specify 3 after -k option as shown below.
$ sort -k 3 data2.txt 1 xyz 178 2 def 234 3 abc 943
Please note, the columns are numbered from 1, starting from the leftmost column. The next column is 2, next one is 3 and so on.
4. Save sort output to file
You may also save the sorted output to another or same file with -o command. Just mention the file path to which the sort command’s output needs to be stored after the -o option and before the file to be sorted.
$ sort -o new_data.txt data.txt $ cat new_data.txt 21 232 232323 32124 334343 34343 576767
Please note, if you don’t specify the full file path in any of the above commands, sort command will look for the file in your present working directory. Also, is you want to modify the input file, just mention it after -o option.
$ sort -o data.txt data.txt $ cat data.txt 21 232 232323 32124 334343 34343 576767
5. Sort Command output
You may also pass the output of commands to sort command using | (pipe) operator, as shown below. In this example, we reverse sort the output of ls command using sort.
$ ls abc.txt conf data.txt data1.txt data2.txt gpush.sh logo-transparent.png mysql_backup.sh new_data.txt postit.sh remove-old-snaps t tweet.py tweet_list $ ls | sort -r tweet_list tweet.py t remove-old-snaps postit.sh new_data.txt mysql_backup.sh logo-transparent.png gpush.sh data2.txt data1.txt data.txt conf abc.txt
The difference between sorting a file vs sorting a command output is that when you sort command output, you need to mention sort command after the command whose output you want to sort. In this case, we mention sort command after ls command and not the other way round. In case of sorting files, we mention sort keyword first, followed by filename.
Also read:
How to Configure PAM in Linux
How to Read Large CSV File in Python
How to Get Filename from Path in Python
How to Increase SSH Connection Timeout
How to Run Sudo Command without Password
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.