title: Timestamp arithmetic in bash
body: <p>Say I have two log files (<code>input.log</code> and <code>output.log</code>) with the following format:</p>
  <pre><code>2012-01-16T12:00:00 12345678
  </code></pre>
  <p>The first field is the processing timestamp and the second is a unique ID. I'm trying to find:</p>
  <ol>
  <li>The records from <code>input.log</code> which don't have a corresponding record for that ID in <code>output.log</code></li>
  <li>The records from <code>input.log</code> which have a record for that ID, <em>but</em> the difference in the timestamps exceeds 5 seconds</li>
  </ol>
  <p>I have a <a href="https://gist.github.com/1623177" rel="nofollow noreferrer">workaround solution with MySQL</a>, but I'd ideally like to remove the database component and handle it with a shell script.</p>
  <p>I have the following, which returns the lines of <code>input.log</code> with an added column if <code>output.log</code> contains the ID:</p>
  <pre><code>join -a1 -j2 -o 0 1.1 2.1 &lt;(sort -k2,2 input.log) &lt;(sort -k2,2 output.log)
  </code></pre>
  <p>Example output:</p>
  <pre><code>10111 2012-01-16T10:00:00 2012-01-16T10:00:04
  11562 2012-01-16T11:00:00 2012-01-16T11:00:10
  97554 2012-01-16T09:00:00
  </code></pre>
  <p><strong>Main question</strong>:</p>
  <p>Now that I have this information, how can I go about computing the differences between the 2 timestamps and discarding those over 5 seconds apart? I hit some problems processing the ISO 8601 timestamp with <code>date</code> (specifically the <code>T</code>) and assumed there must be a better way.</p>
  <p><sub><em>Edit: GNU coreutils supports ISO 8601 since late 2011, not long after this question was asked. This is likely no longer an issue for anyone. See <a href="https://unix.stackexchange.com/a/107773/8742">this answer</a></em></sub></p>
  <p><strong>Secondary question</strong>:</p>
  <p>Is there perhaps a way to rework the entire approach, for instance into a single <code>awk</code> script? My knowledge of processing multiple files and setting up the correct inequalities for the output conditions was the limiting factor here, hence the approach above.</p>