EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Unix Tutorial Sort Command in Unix
 

Sort Command in Unix

Priya Pedamkar
Article byPriya Pedamkar

Updated May 31, 2023

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Following are the features of the sort command:

SORT command in Unix helps arrange or sort a file’s data by line-by-line. The sort command sorts lines of text files and functions 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.
By default, blank space is taken as the field separator. Unix sorting typically relies on extracting one or more keys from the input contents to perform the sorting.

What Can We Do From Sort Command?

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

  • 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:

In Unix, you write the sort command with the options explained above, along with the file on which you want to apply the sort operation. The basic syntax of the sort command is given below:

sort [options] [files]

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:

The sort command first sorts all the uppercase data, followed by the lowercase data.

sort testing.sh

Sort Command in Unix-1.3

Options with Sort Command

You can use the sort command with options 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

Option -k

6. Option -c

The ‘-c’ option checks if the contents in the files are sorted or not and outputs the result if they are not sorted. If there is no output, that means the contents in the files are already sorted. Additionally, it will provide the line number where the content is not sorted.

Syntax:

sort -c file_name.txt

Example:

sort -c testing.sh
cat testing.sh

Option -c

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

Option -u

8. Option -M

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

Syntax:

sort -M file_name.txt

Example:

cat month_list.txt
sort -M month_list.txt

Option -M

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. Note that in Unix, a blank space is considered a field separator.

Recommended Articles

We hope that this EDUCBA information on “Sort Command in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. For Loop in Unix
  2. Crontab in Unix
  3. Array in Unix
  4. VI Editor in Unix
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

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

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW