Wednesday, 22 October 2014

The Easiest Way Ever to Compare 2 Texts

Here's another reason to use Linux: You can use the beautiful terminal tool called wdiff (more useful when only certain words in a sentence may have changed, as opposed to whole sentences or code, in which case, diff is a better tool)

How do you use it? First, you'll have to install it (simple standard "
sudo apt-get install wdiff" is enough - HA! Let's see Windows install anything that easily!)

Then, just use this crazy little command whenever you need to compare two documents:

echo "Enter text1 (press Enter,Ctrl+D when done):"; cat > /tmp/1.txt; echo "Enter text2 (press Enter,Ctrl+D when done):"; cat > /tmp/2.txt; echo "Comparing..."; wdiff -n -w $'\033[1;31m' -x $'\033[0m' -y $'\033[1;32m' -z $'\033[0m' -s /tmp/1.txt /tmp/2.txt; rm /tmp/1.txt; rm /tmp/2.txt

Or, let's just make our lives easier and put all of that into a function (therefore, making it more nicely written and easier to understand too)

function 2compare {
  echo "Enter text1 (press Enter,Ctrl+D when done):"
  cat > /tmp/1.txt
  echo "Enter text2 (press Enter,Ctrl+D when done):"
  cat > /tmp/2.txt
  echo "Comparing..."
  wdiff -n -w $'\033[1;31m' -x $'\033[0m' -y $'\033[1;32m' -z $'\033[0m' -s /tmp/1.txt /tmp/2.txt
  rm /tmp/1.txt
  rm /tmp/2.txt
}
Now, all you have to do is call 2compare and you will be able to compare texts.

Well, let's break it down, shall we?

The echo parts just show useful messages on the screen.
The cat parts store the data taken as input into temporary locations /tmp/1.txt and /tmp/2.txt
The wdiff part is the scariest bit but is quite simple (as we shall see):
  • -n is a short way of saying --avoid-wraps which means "do not extend fields through newlines"
  • -w $'\033[1;31m' -x $'\033[0m' -y $'\033[1;32m' -z $'\033[0m' sets up colours to show the differences in a much more easy to see way. wdiff by default shows colourless output (and shows differences using brackets etc.)
  • -s or --statistics causes wdiff to show how many words were added, deleted etc.
  • /tmp/1.txt /tmp/2.txt just specifies the files to be compared
The rm parts remove the temporary files

But what if you don't want to keep rewriting that crazy long function each time you restart your terminal?
You can place it at the end of ~/.bashrc and then you'll be able to use 2compare directly from your terminal next time onwards.

This command is staying in my ~/.bashrc probably permanently from now on. Maybe I'll post some other useful stuff I have in there in later blog posts.

Do you know of any other ways of comparing files? Any new terminal tricks?
Leave your comments below.

Thursday, 2 October 2014

Going Backwards

All the time, we are told to only keep going forwards - keep looking towards the bright future and to strive to achieve greater goals. How many times are we told that we sometimes need to move backwards?

Think about a scenario where you have to jump from one building to another - you'll never jump directly from the parapet; you'll definitely move a few steps backwards to be able to take a run up in order to make a successful long jump. This applies to real life as well (in a metaphorical sense, of course). Many a times, we really need to move backwards to reassess and rethink whatever we are doing in order to be able to do a whole lot better than before. These are times when we need to backtrack whatever we've been doing or working on and start afresh.

Backtracking is hard since it goes against the whole fiber of who we have been molded into by society itself. It is difficult to let go of whatever you've worked on for a helluva long time but sometimes, its just better to take a few steps back in order to be able to get a better run up and a better jump and just maybe reach a much greater goal and dream bigger.

What do you think about this metaphor? Leave your comments below.

Wednesday, 1 October 2014

Negabinary

Thought you knew all there is to know about number systems? Well, here's something new to that list - Negabinary numbers. These are numbers that are written to the base -2 (negative two). What's so great about these numbers? There is no need to put any + or - to denote whether it is a positive number or not. Let's check these numbers out.

The normal way a number is defined, works here as well. There are "b" number of digits in a number system of base "b". Here, we just have to change the definition to: there are "|b|" number of digits in a number system of base "b". The definition works perfectly after that. Then, we can just use the following equation:

Using this we can thus generate the first few numbers (0, 1, 2, 3, 4, 5...) in negabinary: 0, 1, 110, 111, 100, 101... (Sequence A039724 on Online Encyclopedia of Integer Sequences (OEIS)) where 0 and 1 have usual face values.

How do we convert a number to this base then? Write the number down in binary first. Then, going from right to left, at every odd place (i.e. x1, x3, x5 ...), if the bit is one, propagate a carry to the next bit. Also, while moving from right to left, add all the carries.

For example:

To convert 15 to negabinary:
  1. Convert 15 to binary
    • 15decimal=1111binary
  2. Propagate from right to left
    • 1111
    • 1111
    • 1211
    • 2011
    • 2011
    • 10011
    • 10011
    • 10011negabinary
  3. Done! (This is correct since 16-0+0-2+1 = 15)
Conversion to decimal (or any other base) just uses the original formula.

Addition is kinda done the way conversion is done, except that carries are propagated (one bit extra to the left, since 2negabinary = 110).

We can similarly define the other operations as well.

Let's take this onto another level itself: how about a number system that takes care of complex numbers as well (as digit strings)? I leave that upto you to figure out and comment about.

Sunday, 28 September 2014

Reaching the network's speed limit for file transfer

Are you tired of transferring files from one computer to another using pen-drives and their abysmally slow speeds? Ever long for something faster? Here's a nice way to transfer files insanely fast - as fast as the network would allow (Note: This is another cool reason to switch to Linux)

Here's what you do:

Let's say you want to send a folder called ABC from computer X to computer Y. Then, fire up terminal on computer Y first and type the following commands:
hostname -I 

nc -l 9898 | tar xv

"hostname -I" shows all IP addresses associated with the host Y. You'll need to note these down to be used on computer X. "nc" is a utility that allows for arbitrary TCP and UDP connections and listens. "-l 9898" is a flag for nc to open port 9898 and listen for connections. The "|" pipes the output of nc (i.e. whatever comes from the network) into the next command "tar" which is an archival utility. The "xv" part stands for extract verbose which means that the archive coming from nc is extracted and each file name is printed out as it is extracted.

Once you're done running the above command on Y, run the following command on X:
tar c ABC | nc IP 9898

The "tar c" part stands for "use tar to compress" ABC (the folder to be sent). IP is the IP address got from the "hostname -I" command on computer Y. "nc IP 9898" connects to computer Y on port 9898 and sends the data from the tar command to the other computer.

Since no extra data is transferred in this way, the data should be transferred at the maximum (theoretical) limit of your network. If your network is unstable, this might actually cause a problem since no error correction codes are sent; however, if your network is stable (or all you are sending is movies or the like) then it shouldn't matter much.

Do you know of any other fast ways to transfer data on the network? Leave a comment below. :)

Friday, 26 September 2014

Shellshock

A newly discovered bug seems to be taking the whole security and hacker communities into uproar. A huge number of posts and talks are going on in many channels. For those who haven't heard about it, it is the bash bug which affects basically almost all systems that the world depends on.

What is the bash bug? To answer that, first you need to know what bash is.

"bash" stands for "Bourne-Again SHell" and is the most common type of shell on any linux or mac or unix related system. This basically includes almost all servers, and can go on all the way to smart lighting (those crazy lightbulbs whose colour can be controlled by your smart phone). If you've ever seen anyone use a black screen with white text on it, chances are you've seen either the Windows Command Prompt or a shell. A shell basically allows you to type commands to execute programs on a computer.

The bash bug is basically a bug that has been found in this very commonly used shell. The bug was discovered by Stéphane Chazelas, a French IT manager working for a software maker in Scotland, and was first disclosed on 24th September 2014. It basically allows for arbitrary code execution. Turns out that this bug has existed since the very first version of bash (25 years ago!!!). The bug has been nicknamed "Shellshock" and is regarded to be severe since CGI scripts using bash can be vulnerable. It is caused due to

However, this is where the open source community comes into play. Within a very short time frame, a patch has been released already and many systems are no longer vulnerable to this bug. As of now, my system is no longer vulnerable only because I continuously keep updating my PC.

How to test if you're vulnerable?
Just run the following code in your terminal:
env x='() { :;}; echo vulnerable' bash -c "echo this is a test"

If your system is vulnerable, it'll tell you vulnerable, otherwise it will show an error message.

If you want to know more, visit the Wikipedia page on the bug at Shellshock

Just to mention this here: This is the only severe bug I've personally seen on linux that has such a massive impact, and even then, it got fixed almost instantly. I love the way the open source community works so quickly. :)

Sunday, 6 April 2014

Dreams - A connection to a different time

Dr. A. P. J. Abdul Kalam once said "You have to dream before your dreams can come true." This started to make me think, what if it is not just applicable to thinking big and achieving what you want? What if just normal dreams too work this way? Think about it, has it not happened that you are in some place and you just realize that it all had happened in a dream before? Kind of like déjà vu but not quite the same.

Déjà vu, is the experience of thinking that a new situation has occurred before. I am not talking about this but am talking about the phenomenon where we know we’ve been through the same situation in a dream.

It is common to dream about the past and relive some of the emotional moments that we’ve had until now, but is there any sense in thinking about dreaming about the future? Do we really dream about what will happen in a day or week or year and does it really happen?

We all know that when we’re in a dream, we do not really know that we’re in that dream. It is thus quite possible that we’re thinking that we’ve already been in a place in a dream when we’re in a dream. But all this becomes too much like inception (the movie, of course). Let’s just stay away from the dream inside a dream inside a dream thing.

If we look at what is happening objectively, there are plenty of reasons why we may come across this phenomenon. One possible explanation is that we may just be thinking that we’ve been there in a dream whilst never having such a dream ever before. This is quite possible since our brains are quite unreliable in terms of memories, especially when it comes to dreams. It is quite well known that we cannot remember clearly what we dreamt about even just after waking up. We may just remember pieces of it and this too is fast fading. Is it not possible then, that the mind, when in some situations, can trigger partial recall and patch up pieces from different unrelated dreams into a dream coherent with the current reality?

This’d then mean that the phenomenon would just be a post hoc thing that the brain does and that predicting the future is not really possible.

However, consider the following scenario. You are late for work, and you know the boss is going to scream at you. It is no surprise then that s/he screams at you for being late. Your mind “predicted” the future. Is it not possible that the mind, in dreams, is doing something similar? It is known that the mind has quite a high amount of activity even when we sleep and this increases significantly during dreams. We can say that when awake, our conscious mind is in power, but when dreaming, it’s the unconscious.

The unconscious mind may, in fact, have the ability to think rationally when the conscious mind cannot. It thus may be able to piece together clues that the conscious mind may be ignorant of or may be ignoring and might be able to construct possible case scenarios. Showing the scenarios in dreams may be a way of the brain letting us prepare for what’s about to come. When we realize that a certain situation occurred in a dream, we may even realize what we did in the dream and be able to take the same course of action, if it was helpful, or take a different action if otherwise.

A third possibility exists, however. That the “real world” is affected in a much more powerful way by dreams. A dream may have the capacity to affect real world decisions to such an extent that we may not really have any free will. We may be forced to take decisions that’ll eventually lead us to fulfilling those dreams. A scary thought to those who have nightmares! Lazy people, however, would like this scenario since it’d mean that they’d just have to dream big to achieve big.

We may never know the true nature of dreams and whether they present windows to the future or affect it. However, we know that dreams present windows to another world that is sometimes filled with possibilities that may not be possible in reality.

Dreams are and continue to remain a fascinating, captivating, mesmerising and enchanting way to see the countless possibilities that exist in our universe and any others beyond. They continue to be a source of endless debates and a source of creative ideas for both art and the sciences. Just ask Kekulé who figured out that benzene must have a cyclic ring structure by dreaming of a snake biting its own tail!

The Square Root of Three by Dave Feinberg

I fear that I will always be
A lonely number like root three

A three is all that's good and right
Why must my three keep out of sight
Beneath a vicious square-root sign?
I wish instead I were a nine

For nine could thwart this evil trick
With just some quick arithmetic

I know I'll never see the sun
As one point seven three two one

Such is my reality
A sad irrationality

When, hark, just what is this I see?
Another square root of a three

Has quietly come waltzing by
Together now we multiply
To form a number we prefer
Rejoicing as an integer

We break free from our mortal bonds
And with a wave of magic wands

Our square-root signs become unglued
And love for me has been renewed

-- Dave Feinberg