Wednesday, January 23, 2013

gawk - Calculate date / time difference between timestamps

How to find the time difference between timestamps using gawk?
  Let us consider a file where the
        1st column is the Process name,
        2nd is the start time of the process, and
        3rd column is the end time of the process.

The requirement is to find the time consumed by the process which is the difference between the start and the end times.

1. File in which the date and time component are separated by a space:
$ cat file
P1,2012 12 4 21 36 48,2012 12 4 22 26 53
P2,2012 12 4 20 36 48,2012 12 4 21 21 23
P3,2012 12 4 18 36 48,2012 12 4 20 12 35
Time difference in seconds:
$ awk -F, '{d2=mktime($3);d1=mktime($2);print $1","d2-d1,"secs";}' file
P1,3005 secs
P2,2675 secs
P3,5747 secs
  Using mktime function, the Unix time is calculated for the date time strings, and their difference gives us the time elapsed in seconds.

Monday, January 21, 2013

gawk - Date and time calculation functions

gawk has 3 functions to calculate date and time:
  • systime
  • strftime
  • mktime
   Let us see in this article how to use these functions:

systime:
   This function is equivalent to the Unix date (date +%s) command. It gives the Unix time, total number of seconds elapsed since the epoch(01-01-1970 00:00:00).
$ echo | awk '{print systime();}'
1358146640
Note: systime function does not take any arguments.

Thursday, January 17, 2013

What is UNIX time?

1. What is Unix Time?
  Unix time is total number of seconds measured since  01-Jan-1970 00:00:00.  This time format is used in all the Unix flavors for time computing activities such as dates associated with files/inodes, etc. This date , 1-Jan-1970 00:00:00 is also called epoch date.

2. How to find the Unix time ?
    The date command with %s option :
$ date '+%s'
1357906744
  This number is the total number of seconds elapsed since 01-Jan-1970 till now.

Tuesday, January 15, 2013

Perl - Split a string into individual character array

How to split a string into individual characters and store in an array in Perl? Let us see in this article how to split a string 'welcome' into separate characters.

1. Using the split function:
my $x='welcome';
my @arr=split (//, $x);
print "@arr";
    The split function is used to split the string $x by giving the delimiter as nothing.  Since the delimiter is empty, split splits at every character, and hence all the individual characters are retrieved in the array.
  On running the above snippet, the result produced will be:
w e l c o m e

Wednesday, January 2, 2013

Perl - How to print a hash?

How to print the contents of a hash? Let us see in this article the different options to print a hash.

1. Using foreach and keys:
#!/usr/bin/perl

use warnings;
use strict;

$\="\n";

my %h1=(Jan => 31, Feb => 28, Mar => 31);

foreach my $key (keys%h1) {
        print "$key => $h1{$key} ";
}
   Using the keys function, the keys are generated and processed in a loop. In every iteration, the key and its corresponding value is printed using the $key variable which holds the key.