• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, May 25, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

7 Interesting Linux ‘sort’ Command Examples – Part 2

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
25
VIEWS
Share on FacebookShare on Twitter

In our last article we have covered various examples on sort command, if you’ve missed, you can go through it using below link. In continuation to the last post this post aims at covering remaining of sort command so that both the article together serves as complete guide to Linux ‘sort‘ command.

  1. 14 ‘sort’ Command Examples in Linux

Before we continue further, create a text file ‘month.txt‘ and populate it with the data as given below.

$ echo -e "mar/ndec/noct/nsep/nfeb/naug" > month.txt
$ cat month.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

15. Sort the file ‘month.txt‘ on the basis of month order by using switch ‘M‘ (–month-sort).

$ sort -M month.txt

Important: Note that ‘sort‘ command needs at least 3 characters to consider month name.

7 Interesting Linux ‘sort’ Command Examples – Part 2


16.
Sort the data that is in human readable format say 1K, 2M, 3G, 2T, where K,M,G,T represents Kilo, Mega, Giga, Tera.

$ ls -l /home/$USER | sort -h -k5

7 Interesting Linux ‘sort’ Command Examples – Part 2

17. In the last article we have created a file ‘sorted.txt‘ in example number 4 and another text file ‘lsl.txt‘ in example number 6. We know ‘sorted.txt‘ is already sorted while ‘lsl.txt‘ is not. Lets check both the files are sorted or not using sort command.

$ sort -c sorted.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

If it returns 0, means that the file is sorted and there is no conflict.

$ sort -c lsl.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

Reports Disorder. Conflict..

18. If the delimiter (separator) between words are space, sort command automatically interpret anything after horizontal space as new word. What if the delimiter is not space?

Consider a text file, the contents of which are separated by anything other than space such as ‘|’ or ‘/’ or ‘+’ or ‘.’ or….

Create a text file where contents are separated by +. Use ‘cat‘ to check the contents of file.

$ echo -e "21+linux+server+production/n11+debian+RedHat+CentOS/n131+Apache+Mysql+PHP/n7+Shell Scripting+python+perl/n111+postfix+exim+sendmail" > delimiter.txt
$ cat delimiter.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

Now sort this file on the basis of 1st field which is numerical.

$ sort -t '+' -nk1 delimiter.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

And second on the basis of 4th field which is non numeric.

7 Interesting Linux ‘sort’ Command Examples – Part 2

If the delimiter is Tab you may use $’/t’ in place of ‘+’, as shown in the above example.

19. Sort the contents of ‘ls -l‘ command for your home directory on the basis of 5th column which represents the ‘amount of data‘ in Random order.

$ ls -l /home/avi/ | sort -k5 -R 

7 Interesting Linux ‘sort’ Command Examples – Part 2

Every time you run the above piece of script you are likely to get a different result since the result is generated randomly.

As clear from the Rule number – 2 from the last article, sort command prefer line starting with lowercase characters over uppercase characters. Also check example 3 in last article, where string ‘laptop‘ appears before string ‘LAPTOP‘.

20. How to override the default sorting preference? before we are able to override the default sorting preference we need to export the environment variable LC_ALL to c. To do this run the below code on your Command Line Prompt.

$ export LC_ALL=C

And then sort the text file ‘tecmint.txt‘ overriding the default sort preference.

$ sort tecmint.txt
7 Interesting Linux ‘sort’ Command Examples – Part 2

Override Sorting Preferences

Don’t forget to compare the output with the one you achieved in example 3 and also you can use option ‘-f‘ aka ‘–ignore-case‘ to get much organized output.

$ sort -f tecmint.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

21. How about running ‘sort‘ on two input files and join them in one go!

Lets create two text file namely ‘file1.txt‘ and ‘file2.txt‘ and populate it with some data. Here we are populating ‘file1.txt‘ with numbers as below. Also used ‘cat‘ command to check the content of file.

$ echo -e “5 Reliable/n2 Fast/n3 Secure/n1 open-source/n4 customizable” > file1.txt
$ cat file1.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

And populate second file ‘file2.txt‘ with some data as.

$ echo -e “3 RedHat/n1 Debian/n5 Ubuntu/n2 Kali/n4 Fedora” > file2.txt
$ cat file2.txt

7 Interesting Linux ‘sort’ Command Examples – Part 2

Now sort and join the output of both the files.

$ join <(sort -n file1.txt) <(sort file2.txt)

7 Interesting Linux &#8216;sort&#8217; Command Examples &#8211; Part 2

That’s all for now. Keep Connected. Keep to Tecmint. Please Provide us with your valuable feedback in the comments below. Like and share us and help us get spread

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

6 Interesting Funny Commands of Linux (Fun in Terminal) – Part II

Next Post

4 Useful Tips on mkdir, tar and kill Commands in Linux

Next Post

4 Useful Tips on mkdir, tar and kill Commands in Linux

Leave a Reply Cancel reply

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

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet