sort command in Linux with Examples

Introduction

sort command in Linux is a powerful utility that organizes text lines in a file or input stream. Whether you’re managing log files, organizing data, or processing large datasets, sort offers robust sorting capabilities, from simple alphabetical order to complex numerical arrangements. This guide delves into the sort command, showcasing its versatility with practical examples and tips for advanced usage.

Understanding the Basics of the sort Command

What is the sort Command?

The sort command in Linux arranges lines of text in a specified order. It supports multiple options for sorting, including numerical, alphabetical, case-insensitive, and more.

Syntax

sort command in Linux with Examples

Syntax

sort [OPTION]… [FILE]…

On the man page, the describes it

  • sort – sort lines of text files
  • man sort – More details information about sort command.

sort command in Linux with Examples

$ sort devopsroles.txt

Key Features of the sort Command

Primary Options

  • -n: Numerical sort.
  • -r: Reverse order.
  • -k: Sort by a specific column.
  • -t: Specify a delimiter for field separation.
  • -u: Remove duplicate lines.

Combining Options

The sort command allows combining multiple options to achieve complex sorting tasks.

Examples of Using the sort Command in Linux

1. Sorting Alphabetically

Example:

sort file.txt

This sorts the lines in file.txt alphabetically.

Input:

banana
apple
cherry

Output:

apple
banana
cherry

2. Numerical Sorting

Example:

sort -n numbers.txt

Sorts the file numerically.

Input:

10
2
30

Output:

2
10
30

3. Reverse Sorting

Example:

sort -r file.txt

Sorts the lines in reverse alphabetical order.

4. Sorting by a Specific Column

Example:

sort -k 2 data.txt

Sorts the file based on the second column.

Input:

John 25
Alice 30
Bob 22

Output:

Bob 22
John 25
Alice 30

5. Sorting with a Delimiter

Example:

sort -t: -k 2 scores.txt

Sorts the file by the second field, with : as the delimiter.

Input:

Alice:85
Bob:90
John:78

Output:

John:78
Alice:85
Bob:90

6. Removing Duplicates

Example:

sort -u file.txt

Removes duplicate lines during sorting.

7. Handling Large Files

For large files, use the -T option to specify a directory for temporary storage:

sort -T /tmp largefile.txt

Advanced Techniques with the sort Command

Sorting with Case Insensitivity

Example:

sort -f file.txt

Sorts lines without considering case sensitivity.

Merging Sorted Files

Example:

sort -m file1.txt file2.txt

Combines two pre-sorted files into one sorted output.

Sorting Based on Month Names

Example:

sort -M months.txt

Sorts lines based on month names (e.g., Jan, Feb).

Sorting with Unique Keys

Combine sort with uniq for advanced deduplication:

sort file.txt | uniq

Frequently Asked Questions (FAQs)

1. How do I sort files in reverse order?

Use the -r option:

sort -r file.txt

2. Can sort handle case-insensitive sorting?

Yes, the -f flag enables case-insensitive sorting.

3. How do I sort large files efficiently?

Use the -T option to specify a temporary directory with sufficient space.

4. What does the -k option do in sort?

The -k option sorts based on a specified column or field.

5. How do I sort files numerically?

Use the -n option:

sort -n file.txt

External Resources

Conclusion

sort command is a simple command in Linux. It is the most popular in use terminal Linux sort lines of text files. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.