1

I have a command response that looks something like that:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXX  |   XXX | Thu Oct 18 03:32:00 UTC 2018

XXX  |   XXX | Thu Oct 18 03:32:00 UTC 2018

There are a lot of tab spaces between each set of characters and pipe. The aim is to pick out the date from the response and then compare it with the current time, find the difference and make sure it's not more than 300 seconds. This is my first time making a shell script and I am not sure how this could work.

I have tried using grep to get the entire line. Is there a way to neglect the spaces in between the characters? How do I convert that format of a date to the regular date format?

  • Oh no, it can be different, thank you for pointing it out. It's usually different by a few seconds. – Aman Deep Middha Oct 19 '18 at 0:06
  • Partial answer: you can use the following to grab the third field, i.e. the date cut -d'|' -f 3 – Sparhawk Oct 19 '18 at 0:07
  • I want the time difference between the current time and the time on the response. It shouldn't be delayed basically. For the current time, I am making a variable in my shell script. – Aman Deep Middha Oct 19 '18 at 0:10
  • I have to do it for every response in the file. There are actually 10 responses which can be similar or differ by a few seconds. – Aman Deep Middha Oct 19 '18 at 0:20
0
#!/bin/bash
grep 'UTC\|EDT\|EST' file | cut -d' ' -f 8 | while IFS= read line; do

now=$(date | cut -d' ' -f 4)

time_now=$(date -u -d "$now" +"%s")   #seconds
time_line=$(date -u -d "$line" +"%s") #seconds

difference=$(echo $((time_now - time_line)))

  if [ $difference -lt 300 ] 

     then
       echo "Time difference is less than 300 seconds"

     else 
       echo "Time difference is more than 300 seconds"
   fi

done
  • Your question is` ..... pick out the date from the response and then compare it with the current time, find the difference and make sure it's not more than 300 seconds` that means you need the difference between the time in the response and the current time. You confirmed this also in the comments above. This has nothing to do with oct or Nov? – user88036 Oct 19 '18 at 1:14
  • time_line shows a time that's much ahead of the current time (450 years actually), that's the problem. Is there a fix to that? – Aman Deep Middha Oct 19 '18 at 2:58
  • @AmanDeepMiddha the calculation of time_line is in seconds not in years. There a fix of everything just clarify what you want in the question and happy to help ;-) – user88036 Oct 19 '18 at 9:40
0

Here you get the difference of current dat/time to every date in file, in seconds:

sed -rn '/([[:alpha:]]{3} ){2}([ [:digit:]]{2}[ :]){4}/ s/^.*\|[[:space:]]*//p' file | date +"echo \$(( $NOW - %s ))" -f- | sh
109431
109431

The output format, and what to do to it, is to be specified...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.