3

There are numerous programmes (e.g. ts in moreutils), and various bash one-liners to prepend the output of a command/pipe with the timestamp of when that line was 'printed'.

However is there a command that, for each line that it gets, will print the time since the command started (and/or when it first saw a line), and the current line? So rather than print the current time, it'll print how long since that command started? (aswell as the line aswell)

When running commands that take a while, and spew a lot of output, it can be helpful to know how long you've been looking at it.

It would be trivial to whip a simple command up, but I don't want to re-invent the wheel.

This is on an Ubuntu Linux system.

2 Answers 2

4

Here's one way:

#!/bin/bash
SECONDS=0    # it will already be initialized to zero at the start of the script
while read -r line
do
    echo "$SECONDS: $line"
done < file    # or < <(command)

SECONDS is a variable that automatically increments every second.

0

Well, it looks like there are under-documented (missing on some man pages) options to ts that do exactly what you want.

From my system's man ts:

If the -i or -s switch is passed, ts reports incremental timestamps instead of absolute ones. The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used as well. In case of -i, every timestamp will be the time elapsed since the last timestamp. In case of -s, the time elapsed since start of the program is used.

The -m switch makes the system's monotonic clock be used.

Not sure if these existed when you asked but man says "Copyright 2006".

Example

cameron@linux:~$ (echo a; sleep 1; echo b; sleep 2; echo c) | ts -i 'delta: %.S' | ts -s 'abs: %.S'
abs: 00.000007 delta: 00.000009 a
abs: 00.973008 delta: 00.972945 b
abs: 02.974591 delta: 02.001582 c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .