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.

Tuesday, April 5, 2011

sed - Read from a file or write into a file

  In this sed article, we will see how to read a file into a sed output, and also how to write a section of a file content to a different file.

  Let us assume we have 2 files, file1 and file2 with the following content:
$ cat file1
1apple
1banana
1mango
$ cat file2
2orange
2strawberry
  sed has 2 options for reading and writing:

Monday, March 28, 2011

Different ways to add header and trailer line to a file

   In this article, we will see the different ways to add a header record or a trailer record to a file.

Let us consider a file, file1.
$ cat file1
apple
orange
grapes
banana