Posted: 9 September 2006 at 7:56 pm by Davey
Article note: This article appeared originally on shintara way back in 2006.
The power of the UNIX shell is one that any UNIX or UNIX variant operating system user should be taking full advantage of. One such function is called redirecting - in particular redirecting text from a source and filtering it to your needs in a destination.One such example of this happened to me while on a customers site and dealing with someone from a competitor company who was trying to outsmart me. I was relying heavily on a flat text file that was being sent to our equipment for parsing, in short I needed to write some macros for this to work effectively - this was dependant on the text that I was recieving which should never have changed.
The customer supplied me with a whole lot of garbage text for the macros and I needed to sort the garbage from the good. I knew the deliminating word that would help with this, it was xray - anything without the text xray didnt mean anything and was irrelevant. I quickly hopped onto a shell and typed the following command:
-bash-2.05b$ grep xray ~/xrayfile.txt > ~/xrayfilegood.txt
Lets break this down,
- grep then returns all the lines that contain the string xray in the file xrayfile.txt
- (the tilde ~ is a shortcut for your home directory, hence ~/xrayfile.txt would be /home/user_name/xrayfile.txt).
- We then redirect > this output into the xrayfilegood.txt.
The result was a new text file called xrayfilegood.txt which resided in my home directory, within this file all the text that I needed to make my macros. Easy as that!
The raw power of redirecting text emerged the next day when a person from a competitor company was working on his system that sent the flat file of text to our equipment, not knowing that I knew the power of redirecting he changed the text coming into our workstation thus rendering the macros that I had made useless.
A quick return to the command line to redirect some more text and I was home free to quickly set up some new macros and return our system back to a working state! Moral of the story is, never mess with a person that knows how to redirect!

