Tuesday, May 31, 2011

cut - cut files with a delimiter

   cut is a very frequently used command for file parsing. It is very useful in splitting columns on files with or without delimiter. In this article, we will see how to use the cut command on files having a delimiter.

Let us consider a sample file, say file1, with a comma as delimiter as shown below:
$ cat file1
Rakesh,Father,35,Manager
Niti,Mother,30,Group Lead
Shlok,Son,5,Student 
 The first column indicates name, second relationship, the third being the age, and the last one is their profession.

Tuesday, May 24, 2011

awk - Read a file and split the contents

 awk is one of the most powerful utilities used in the unix world. Whenever it comes to text parsing, sed and awk do some unbelievable things. In this first article on awk, we will see the basic usage of awk.

The syntax of awk is:

awk 'pattern{action}' file

   where the pattern indicates the pattern or the condition on which the action is to be executed for every line matching the pattern. In case of a pattern not being present, the action will be executed for every line of the file. In case of the action part not being present, the default action of printing the line will be done. Let us see some examples:

Tuesday, May 17, 2011

bc - Unix calculator

  Whenever we need to do some calculation, we open the calculator in the windows. For unix programmers, why to go for windows calculator when we have an in-built one, bc.  bc is a unix command which stands for bench calcultor. Depending on the way we use it, it can either look like a programming language or as a interactive mathematic shell. Let us see in this article how to use bc  to do some simple arithmetic.

   bc can be invoked either as a pipe of other command OR by getting into the interactive mode. Let us start by seeing it from the command line perspective.