Date manipulation in BASH, Perl, Python

Posted on Sat 21 May 2011 by Pavlo Khmel

To compare script language for administration tasks.

Using default installation on Linux (RHEL 5, 6), Solaris (10/9), Mac OS X (10.6.7). Implement in BASH, Perl, Python:
1. Find last string in text file, that match “TEST MESSAGE”
2. Get part of string with date
3. Convert human date to EPOCH format (in seconds)
4. Get current date in EPOCH
5. How many time ago was that message

Note: Default installation without additional modules!

Text file with log:

$ cat phn.log
May 14 17:49:18 phn kernel: e1000: eth0 NIC Link is Down
May 18 12:11:20 phn phn.org.ua: TEST MESSAGE
May 19 17:49:23 phn kernel: e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
May 20 13:09:38 phn phn.org.ua: TEST MESSAGE

BASH
BASH: using GNU program “date” in Linux by default but not on: Solaris, AIX, HP-UX, Mac OS X

#!/bin/bash
echo "### Log check BASH ###"
#
# Get date from log file
#
date_time_from_log=`grep "TEST MESSAGE" ./phn.log | tail -n 1 | awk "{ print $1, $2, $3 }"`
echo "String from log file: $date_time_from_log"
#
# Convert Human date to EPOCH time in seconds
#
date_string_to_seconds=`date -d "$date_time_from_log" +%s`
echo "Date from log in seconds: $date_string_to_seconds"
#
# Date now in EPOCH
#
date_time_now_in_seconds=`date +%s`
echo "Date_time now in seconds: $date_time_now_in_seconds"
#
# Result
#
let result=$date_time_now_in_seconds-$date_string_to_seconds
echo "It happend $result seconds ago"
let result_2=result/60
echo "It happend $result_2 minutes ago"

Perl
CPAN can provide additional modules to make manipulation with date easier. But it should be done with default installation.

#!/usr/bin/perl
use Time::Local;
use POSIX qw(strftime);
print "### Log check Perl ###n";
#
# Get date from log file
#
$srce = "./phn.log";
$string1 = "TEST MESSAGE";
open $fh, $srce or die "Could not open $scre: $!";
@lines = sort grep /Q$string1/, <$fh>;
$last = pop (@lines);
#$first = shift (@lines);
$date_time_from_log = substr($last, 0, 15);
print "String from log file: $date_time_from_logn";
#
# Convert Human date to EPOCH time in seconds
#
$month_string = substr($date_time_from_log, 0, 3);
if ( $month_string eq "Jan" ) { $month_number = 0; }
if ( $month_string eq "Feb" ) { $month_number = 1; }
if ( $month_string eq "Mar" ) { $month_number = 2; }
if ( $month_string eq "Apr" ) { $month_number = 3; }
if ( $month_string eq "May" ) { $month_number = 4; }
if ( $month_string eq "Jun" ) { $month_number = 5; }
if ( $month_string eq "Jul" ) { $month_number = 6; }
if ( $month_string eq "Aug" ) { $month_number = 7; }
if ( $month_string eq "Sep" ) { $month_number = 8; }
if ( $month_string eq "Oct" ) { $month_number = 9; }
if ( $month_string eq "Nov" ) { $month_number = 10; }
if ( $month_string eq "Dec" ) { $month_number = 11; }
$day_from_log = substr($date_time_from_log, 4, 2);
$hour_from_log = substr($date_time_from_log, 7, 2);
$min_from_log = substr($date_time_from_log, 10, 2);
$sec_from_log = substr($date_time_from_log, 13, 2);
$year_now = strftime "%Y", localtime;
$date_string_to_seconds = timelocal($sec_from_log,$min_from_log,$hour_from_log,$day_from_log,$month_number$year_now);
print "Date from log in seconds: $date_string_to_secondsn";
#
# Date now in EPOCH
#
$date_time_now_in_seconds = time();
print "Date_time now in seconds: $date_time_now_in_secondsn";
#
# Result
#
$result = $date_time_now_in_seconds - $date_string_to_seconds;
print "It happend $result seconds agon";
$result_2 = int($result / 60);
print "It happend $result_2 minutes agon";

Python

#!/usr/bin/python
import sys
import time
import datetime
print "### Log check Python ###"
#
# Get date from log file
#
file_name="./phn.log"
searched_string="TEST MESSAGE"
my_string = ""
for line in open(file_name):
if (searched_string) in line:
my_string = line
date_time_from_log = my_string[:15]
print "String from log file: " + date_time_from_log
#
# Convert Human date to EPOCH time in seconds
#
now = datetime.datetime.now()
year_now = " %d" % now.year
date_time = date_time_from_log + year_now
pattern = "%b %d %H:%M:%S %Y"
date_string_to_seconds = int(time.mktime(time.strptime(date_time, pattern)))
print "Date from log in seconds: " + str(date_string_to_seconds)
#
# Date now in EPOCH
#
date_time_now_in_seconds = int(time.time())
print "Date_time now in seconds: " + str(date_time_now_in_seconds)
#
# Result
#
result = date_time_now_in_seconds - date_string_to_seconds
print "It happend " + str(result) + " seconds ago";0D
result_2 = int(result / 60);
print "It happend " + str(result_2) + " minutes ago";

Output

$ ./date_time.sh && ./date_time.pl && ./date_time.py

### Log check BASH ###
String from log file: May 20 13:09:38
Date from log in seconds: 1305889778
Date_time now in seconds: 1305977530
It happend 87752 seconds ago
It happend 1462 minutes ago

### Log check Perl ###
String from log file: May 20 13:09:38
Date from log in seconds: 1305889778
Date_time now in seconds: 1305977530
It happend 87752 seconds ago
It happend 1462 minutes ago


### Log check Python ###
String from log file: May 20 13:09:38
Date from log in seconds: 1305889778
Date_time now in seconds: 1305977530
It happend 87752 seconds ago
It happend 1462 minutes ago