Linux sort Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux sort command to sort files and the output of commands based on different criteria. The lab covers the basics of the sort command, including sorting files in alphabetical or numerical order, and combining sort with other Linux commands for more complex text processing tasks. You will learn how to sort files by different fields or columns, and how to customize the sorting behavior using various options. This lab is suitable for users who want to improve their text processing and editing skills in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/sort("`Text Sorting`") linux/TextProcessingGroup -.-> linux/uniq("`Duplicate Filtering`") subgraph Lab Skills linux/cat -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} linux/head -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} linux/tail -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} linux/grep -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} linux/sort -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} linux/uniq -.-> lab-422927{{"`Linux sort Command with Practical Examples`"}} end

Understand the Basics of sort Command

In this step, we will learn the basics of the sort command in Linux. The sort command is used to sort the lines of a file or the output of a command in a specific order.

To get started, let's create a sample file named data.txt with some unsorted data:

echo -e "apple\norange\nbanana\npear" > data.txt

Now, let's sort the contents of the data.txt file using the sort command:

sort data.txt

Example output:

apple
banana
orange
pear

As you can see, the lines in the file have been sorted in alphabetical order.

The sort command has several options that allow you to customize the sorting behavior. Some common options include:

  • -r: Sort in reverse order (descending)
  • -n: Sort numerically
  • -k <field>: Sort based on a specific field or column
  • -t <delimiter>: Use a custom field delimiter

For example, to sort the file in reverse order:

sort -r data.txt

Example output:

pear
orange
banana
apple

The sort command can also be combined with other Linux commands, such as cat, grep, and pipe, to perform more complex text processing tasks. We'll explore these use cases in the next steps.

Sort Files by Different Criteria

In this step, we will learn how to sort files based on different criteria using the sort command.

Let's create a new file named data2.txt with some sample data:

echo -e "John,25,male\nJane,30,female\nBob,35,male\nAlice,28,female" > data2.txt

Now, let's sort the file by the first field (name):

sort -t ',' -k 1 data2.txt

Example output:

Alice,28,female
Bob,35,male
Jane,30,female
John,25,male

In this example, we used the -t ',' option to specify the field delimiter as a comma, and the -k 1` option to sort based on the first field (name).

You can also sort by numeric values. For example, to sort by the second field (age):

sort -t ',' -k 2n data2.txt

Example output:

John,25,male
Alice,28,female
Jane,30,female
Bob,35,male

The n option in -k 2n tells sort to sort the second field numerically.

Another useful option is -r to sort in reverse order:

sort -t ',' -k 2nr data2.txt

Example output:

Bob,35,male
Jane,30,female
Alice,28,female
John,25,male

The sort command provides many more options to customize the sorting behavior. Experiment with different options to sort your files based on your specific needs.

Combine sort with Other Linux Commands

In this final step, we will explore how to combine the sort command with other Linux commands to perform more advanced text processing tasks.

Let's start by generating a list of random numbers and sorting them:

shuf -i 1-100 -n 10 | sort -n

Example output:

4
12
16
26
31
41
58
67
84
97

In this example, we used the shuf command to generate 10 random numbers between 1 and 100, and then piped the output to the sort command to sort the numbers in numerical order.

You can also combine sort with grep to filter and sort the output:

cat data2.txt | grep 'female' | sort -t ',' -k 2n

Example output:

Alice,28,female
Jane,30,female

This command reads the data2.txt file, filters the lines containing 'female', and then sorts the output by the second field (age) in numerical order.

Another useful combination is sort with uniq to remove duplicate lines:

cat data2.txt | sort | uniq

Example output:

Alice,28,female
Bob,35,male
Jane,30,female
John,25,male

In this example, we first sort the data2.txt file, and then use the uniq command to remove any duplicate lines.

The possibilities are endless when you combine the sort command with other Linux tools. Experiment with different combinations to solve your text processing tasks more efficiently.

Summary

In this lab, we learned the basics of the sort command in Linux, which is used to sort the lines of a file or the output of a command in a specific order. We explored various options to customize the sorting behavior, such as sorting in reverse order, numerically, or based on specific fields. Additionally, we discovered how to combine the sort command with other Linux commands like cat, grep, and pipe to perform more complex text processing tasks. Finally, we learned how to sort files by different criteria, including sorting by name or age, and using custom field delimiters.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like