0

Hey I am trying to calculate the difference between time stamp of a file in which I always want to subtract the last the fields using bash/bash-utils. This is how the file looks

14:11:56.953700000,172.20.10.1 14:25:49.233263000,172.20.10.1

Now the issue is that I want to lose that huge number and IP from the calculation.

I can put them in csv or any data file needed.

  • 1
    It's unclear what you mean by "subtract the last fields", although you could do `awk -F'.' '{print $1}'` to show only the time without nanoseconds. – l'L'l Jun 17 '18 at 09:18
  • @I'L'I as in subtracting those two time stamps to tell what's the difference between both on them in minutes – Coding_Karma Jun 17 '18 at 09:20
  • Use something like that awk command to get the part of the string you want, then [review this thread](https://stackoverflow.com/questions/8903239/how-to-calculate-time-difference-in-bash-script). The question deals with the exact same format as you want... – l'L'l Jun 17 '18 at 10:18
  • 1
    @Coding_Karma, please always add your efforts in your post too what you have put in order to solve your problem. Also by seeing your profile seems like you hardly select any answer as correct, so please always do so(select any one of the answer as correct one) to close the thread completely. – RavinderSingh13 Jun 17 '18 at 10:34

1 Answers1

3

Could you please try following and let me know if this helps you.

awk -F'[.,]' '
FNR==1{
  split($1,time,":");
  sec=time[1] * 3600+time[2]*60+time[3]}
FNR==2{
  split($1,time1,":");
  sec1=time1[1] * 3600+time1[2]*60+time1[3];
  seconds=(sec1-sec)%60;
  min=sprintf("%d",(sec1-sec)/60);
  printf("%s %s\n",min" min",seconds" sec")
}'  Input_file

Output will be as follows.

13 min 53 sec
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93