EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial Sort Command in Unix
Secondary Sidebar
Unix Tutorial
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Sort Command in Unix

By Priya PedamkarPriya Pedamkar

Sort Command in Unix

Introduction to Sort Command in Unix

The SORT command in Unix helps us to sort or arrange the contents in a file in the system, arrange the data in a sorted manner, and redirect the output in a stdout format. The sort command by default helps to sort the file having the contents present in an ASCII manner. By using the options with the sort command, we can also sort the contents in a numeric way or in many other ways.

Following are the features of the sort command:

SORT command in Unix helps to arrange or sort the data of a file by line-by-line. The sort command is used to sort lines of files which are text and acts as a command line utility. This command helps in sorting the contents in an alphabetical or by month or by number or in reverse order and also helps in removing duplicates from the files.
Blank space is taken as the field separator by default. Sorting in Unix is generally done based on one or more keys which is extracted for the contents in the input.

What Can We Do From Sort Command?

Below are some of the list of options that are generally used by sort are listed below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • sort -b: It ignores the blank spaces at the start of the data in the file.
  • sort -r: It reverses the contents of files and sorts the order.
  • sort -o: It specifies and prints the output file in a sorted way.
  • sort -n: It is used to sort the numerical value in ascending order.
  • sort -M: It helps to sort the contents as per the calendar month.
  • sort -u: It helps to remove the duplicates in the file and return the output in a sorted way.

Sort Syntax:

Sort command in Unix is written with options that are explained above and the file for which we would need to operate the sort option. The basic syntax of the sort command is given below:

sort [options] [files]

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,452 ratings)

Examples of Using Sort Command in Unix

Let us suppose we have created a filename called testing.sh and have written few contents in it as below:

We can list the contents in the file by using ‘cat’ command as shown below:

cat testing.sh

Sort Command in Unix-1.1

Now with the help of the sort command, we will sort the data in the file.

Syntax:

sort file_name.sh

Example:

Here we can see that the files have been sort in alphabetical order. By using sort command.

sort testing.sh

Sort Command in Unix-1.2

If we have data in the files both in upper and lower case, by default, the sort command will sort the data first in upper case and then to lower case as shown below:

Example:

All the upper case data is sorted first and then lower case data is followed.

sort testing.sh

Sort Command in Unix-1.3

Options with Sort Command

Sort command with options can be used in several ways. A few of the options for sort are listed below with examples:

1. Option -o

Sort when used with the option ‘o’ helps you to sort the contents into a new file and redirects the output in a sorted form. With the help of option ‘o’, it helps in redirecting the contents to the output file in a sorted form. An example is shown below:

sort testing.sh > outputsh
sort -o output.sh testing.sh
cat output.sh

Sort Command in Unix-1.4

2. Option -r

In Unix, sort command with ‘r’ option gives you to sort the contents in reverse order. This option sorts the given input in a reverse way which is by default in descending order.

Syntax:

sort -r file_name.sh

Example:

sort -r testing.sh

Sort Command in Unix-1.5

3. Option -n

In Unix, when you try to sort a file in a numeric way, you can use the option ‘-n’ with the sort command. This command is used to sort the numeric contents present in the file. Be default, it sorts in ascending order.

Syntax:

sort -r file_name.sh

Example:

cat number.txt
sort -n number.txt

Sort Command in Unix-1.6

4. Option -nr

This option in sort helps to sort the numeric contents in the file in reverse order. By default, the numeric data in reverse order gives the contents in descending order.

Syntax:

sort -nr file_name.txt

Example:

sort -nr number.txt

Sort Command in Unix-1.6..

5. Option -k

The option -k, gives us the option to sort a table on the order of column number. You can give the column number with ‘-k’ to sort the particular column.

Syntax:

sort -k file_name.txt

Example:

cat file1.txt
sort -k2 file1.txt

Sort Command in Unix-1.7

6. Option -c

The option ‘-c’ helps to check if the contents in the files are sorted or not. The output of this option will give us the result if it is not sorted. If there is no output, that means the contents in the files are already sorted. It will also give us the line number where the content is not sorted.

Syntax:

sort -c file_name.txt

Example:

sort -c testing.sh
cat testing.sh

Sort Command in Unix-1.8

7. Option -u

This option when used with sort, helps to remove duplicates and sort the contents in the file. The output produced in the file will give no duplicates and sorted in ascending order by default.

Syntax:

sort -u file_name.txt

Example:

cat fruits.txt
sort -u fruits.txt

Sort Command in Unix-1.9

8. Option -M

Unix also provides us the feature to sort the months in the file. The output that will be given will be the months in a sorted form.

Syntax:

sort -M file_name.txt

Example:

cat month_list.txt
sort -M month_list.txt

Sort Command in Unix-1.10

Conclusion

Sort command in Unix is a very useful and most used command in shell scripting to filter the input contents and to print the output to stdout format. Sort command also helps to sort the contents in alphabetical order, numeric way, reverse order, month wise order, or reverse order. With the combination of the above-mentioned options, you can use the sort command in multiple ways as required by the output. It is to be noted that blank space is considered to be a field separator.

Recommended Articles

This is a guide to Sort Command in Unix. Here we also discuss the definition and what can we do from the sort command along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. For Loop in Unix
  2. Crontab in Unix
  3. Array in Unix
  4. VI Editor in Unix
Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)
  16 Online Courses |  3 Hands-on Projects |  160+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Kali Linux Training (3 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more