offer jobs schedule amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

Sort in Bash Scripting

We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort.

Sort is a simple filter that is used to sort lines of text from its input, input can be your keyboard input, a file, or even an output of another program.

This is its syntax:

sort filename

You simply type sort followed by the name of the file name you wish to sort, or you can simply pipe the result from another program, e.g:

lastlog | sort

Common mistake to avoid when sorting:

When you sort an input and redirect it to another output, you might typically do something like this:

sort file1 > file1

So, in the above example, you basically want to sort the contents of file 1 and replace it with the sorted output. The issue with this command is that once you execute it, you will lose all the data in that file.

What actually happens under the rader is that before the sort program starts, the shell looks at the greater than symbol, and thinks you want to prepare the file for the output that the program is gonna store in it, so, if there is any existing file, it would erase it, and then call the program, but by the time the program runs, you are practically sorting an empty file.

The correct technique would be:

sort file1 > file2
mv file2 file1

So, you sort it, and send the result to another file. You can then rename file2 to file1. You might think this applies to only the sort program, which is not true, it applies to all filters in general, so, you should definitely be careful.

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f