How to sort data in a file in Linux.

Most times we have some of these old files that we need to sort. Usually these files are made without technology in mind. Now we have technology to help us have well organised data.

Image for post
Image for post

Here we are going to do some simple sorting using the Linux OS. So we start with a pretty scattered list of names and we will sort the names in alphabetical order.

Image for post
Image for post

Here we use the sort command to arrange the names in the file alphabetically.
In the above example the file still maintains its scattered form, it only displays its data in alphabetical order.
Most times, we would want a modified file that we will want to use at a later date.

Here, we can transfer the changes to another file using the –o option followed by the name of the file we want to transfer the changes to.

Image for post
Image for post

In this case we are having a scattered list of numbers, using the sort command with the –n option on the file name helps to arrange the numbers in ascending order.

Image for post
Image for post

Here we have a list of months that are scattered. Using the sort command with the –M option on the file name, we can arrange all of the months accordingly.

Hope this was a helpful tutorial.

Software Engineer at mPharma.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store