• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

sort Command Examples in Linux

by admin

The sort command is used to sort the lines of a text file in ascending or descending order, or sort as per a specified key. The sort command arranges the lines in a file. Common sort command options are provided in the table.

Option Description
-k{column numbers} Specify field values. For example, -k2 indicates the second field.
-n Compare and sort lines based on the string numerical value.
-r Sort fields in descending order. By default, the fields are sorted in ascending order.
-t{delimiter} Separate one field from another.

Syntax

The syntax of the sort command is:

# sort [options] {file names}

sort Command Examples

1. Sort a file in ascending order:

# sort path/to/file

2. Sort a file in descending order:

# sort --reverse path/to/file

3. Sort a file in case-insensitive way:

# sort --ignore-case path/to/file

4. Sort a file using numeric rather than alphabetic order:

# sort --numeric-sort path/to/file

5. Sort `/etc/passwd` by the 3rd field of each line numerically, using “:” as a field separator:

# sort --field-separator=: --key=3n /etc/passwd

6. Sort a file preserving only unique lines:

# sort --unique path/to/file

7. Sort a file, printing the output to the specified output file (can be used to sort a file in-place):

# sort --output=path/to/file path/to/file

8. Sort numbers with exponents:

# sort --general-numeric-sort path/to/file

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to configure multicast on an IP address (interface)
  2. What are Bash Exit Codes in Linux
  3. CentOS / RHEL : How to change SNMP community string
  4. How to Install apt-transport-https package in Debian
  5. astyle: Source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages
  6. vmstat: command not found
  7. logsave: command not found
  8. chattr: command not found
  9. “error: Bind to port 2222 on 0.0.0.0 failed: Permission denied” – error while starting sshd service on CentOS/RHEL
  10. How to exclude a file/directory from auditd rules

You May Also Like

Primary Sidebar

Recent Posts

  • “aws workmail” Command Examples
  • aws-vault Command Examples (A vault for securely storing and accessing AWS credentials in development environments)
  • “aws sts” Command Examples
  • “aws sqs” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright