-1

I haved a file like this:

    1633092723, TRANSFERCHECK: OK 
    1633092771, TRANSFERCHECK: OK 
    1633092777, TRANSFERCHECK: OK 
    1633092805, TRANSFERCHECK: OK 
    1633092811, TRANSFERCHECK: OK 
    1633092818, TRANSFERCHECK: OK 
    1633092823, TRANSFERCHECK: OK 
    1633092852, TRANSFERCHECK: OK 
    1633092857, TRANSFERCHECK: OK 
    1633092863, TRANSFERCHECK: OK 
    1633092891, TRANSFERCHECK: OK 
    1633092898, TRANSFERCHECK: OK 
    1633092904, TRANSFERCHECK: OK 
    1633092911, TRANSFERCHECK: OK 
    1633092938, TRANSFERCHECK: OK 
    1633092945, TRANSFERCHECK: OK 
    1633092953, TRANSFERCHECK: OK 
    1633092984, TRANSFERCHECK: OK 

the first colum is the absolut timestamp . How can I make an awk one-liner where the mean time between each abs. timestamp is calculated. It should be used to predicte the total duration of the process. I have done this but this makes the meantime of the timestamps and not the duration from one to the next one.

      cat myfetchlog.log | awk -F, '{sum+=$1} END {print strftime("%c",sum/NR)}'

Thanks a lot. I am an awk beginner and would like to improve.

AdminBee
  • 22,803
  • 5
    Asking for a "one-liner" means you favor brevity over everything that actually matters in software, e.g. clarity, portability, efficiency, robustness, etc. so it discourages many people from posting good answers since you'll probably end up just taking the briefest pile of nonsense that gets posted. So you might want to remove the "one-liner" part from your question. Also, add the expected output for that sample input to your question. – Ed Morton Oct 01 '21 at 14:57

1 Answers1

1

The formula for average of difference is

{(2nd-1st) + (3rd-2nd) + ... + (nth-(n-1)th}/(NR-1)
Here 2nd-1st etc are difference between each consecutive times and there are NR-1 such difference  

So it essentially removes all terms between nth (last term) and 1st (first term) as it becomes (2nd-1st+3rd-2nd+..+nth-(n-1)th)/(NR-1). So the formula is (nth-1st)/(NR-1) Thus awk code is

awk 'NR==1 {first = $1} END {print ($1-first)/(NR-1)}' test

For your sample it returns 15.3529

  • Add a check for NR>1 to avoid a divide by zero error if the input file only has 1 line. Also, using $1 (or any other field or $0) in the END section is undefined behavior per POSIX so that'll work in some awks but will be treated as 0 in others. You need to set a variable to hold $1 on every input line read and then use that in the END section for portability. So while this will produce the expected output for the given sample input with some awks and is a "one-liner" as the OP asked for, it's neither robust nor portable and so will fail with some awks and/or with different input. – Ed Morton Oct 01 '21 at 15:28