Command to Sort File In Reverse Order [Unix/Linux/macOS]


Sort File In Reverse Order Command

To sort a file in reverse order make use of the command sort.

Sort File in Rever Alphabetical Order:

Let's take a text file that has unsorted contents as shown below,

myUnsortedFile.txt
America
Brazil
Japan
China
India
Russia
Ukraine
France
Germany

To sort this file in revere order make use of the sort command with -r option for reverse,

# sort -r myUnsortedFile.txt
Ukraine
Russia
Japan
India
Germany
France
China
Brazil
America

What is the file has lines that contain special characters and numbers,

cat myFile.txt 
America
1
Brazil
#
Japan
@
China
India
!
Russia
Ukraine
2
France
Germany
After Reverse Sorting:
# sort -r myFile.txt
Ukraine
Russia
Japan
India
Germany
France
China
Brazil
America
@
2
1
#
!

Sort File in Rever Numeric Order:

File:
1 America
2 Brazil
3 Japan
4 China
5 India
6 Russia
7 Ukraine
8 France
9 Germany

Make use of -n and -r options together to sort the contents of the file in reverse numeric order,

# sort -nr myFile.txt
9 Germany
8 France
7 Ukraine
6 Russia
5 India
4 China
3 Japan
2 Brazil
1 America
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency