Search This Blog

2013-05-21

Operations in AWK

In awk there are many different mathematical and comparison functions you can perform. Column manipulation is a huge thing and being able to compare values via columns and statements is powerful.

Lets say we want to minus dates/times from two columns. The leftmost being the start time the rightmost being the end time.

This awk statement will check if the right time and the left time is greater than one minute and print the result.

start_and_end_command | awk '{if ($4-$2 > 1) print}'
2013-05-14 13:07 2013-05-14 16:49
2013-05-14 17:50 2013-05-14 19:05
2013-05-15 13:02 2013-05-15 16:44
2013-05-17 16:49 2013-05-17 18:20

Even better yet, We can print the actual length of the reservation by adding a little more logic
start_and_end_command | awk '{if ($4-$2 > 1) print $4,$2,$4-$2}'
16:49 13:07 3 (3 hours)
19:05 17:50 2 (2 hours)
16:44 13:02 3
18:20 16:49 2

No comments:

Post a Comment