shell - Print the difference between 2 timetamps in a file unix -


 cat          row1col1 row1col2 2013-08-26 22:07:26  2013-08-26 22:07:26         row2col1 row2col2 2013-08-26 22:08:16  2013-08-26 22:08:20         (2 rows) 

the objective capture rows have difference of greater 3 seconds between timestamps.

        output here          row2col1 row2col2 2013-08-26 22:08:16  2013-08-26 22:08:20          tried below.          bash-3.2$ echo $d1         08/28/2013 16:56:36         bash-3.2$ echo $d2         08/28/2013 16:56:44         bash-3.2$ date1=$(date -d "$d1" +%s)         bash-3.2$ date2=$(date -d "$d2" +%s)         bash-3.2$ diff=$(expr $date2 - $date1)         bash-3.2$ echo $diff         8           getting syntax error "%s" while using awk command.         here 2 variations tried.but           awk '{t1=$3" "$4;t2=$5" "$6;time2=$(date -d "$t2" +%s);time1=$(date -d "$t1" +%s); if ($(expr $time2 - $time1) > 3) print $0 }' 

awk has built-in mktime() method equivalent of date -d ... +%s except format of date input must "yyyy mm dd hh mm ss".

awk '     {             t1=$3" "$4;             t2=$5" "$6;              gsub(/[-:]/, " ", t1);             gsub(/[-:]/, " ", t2);              time1 = mktime(t1);             time2 = mktime(t2);              if ( ( time2 - time1 ) > 3 )                     print $0     }' 

by way, awk script have mixing shell syntax inside awk script. that's why errors.

for awk without mktime

awk '     {             t1=$3" "$4;             t2=$5" "$6;              cmd1 = "date -d \"" t1 "\" +%s";             cmd2 = "date -d \"" t2 "\" +%s";              cmd1 | getline time1;             cmd2 | getline time2;              if ( ( time2 - time1 ) > 3 )                     print $0     }' 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -