-1

I have this log:

30-12-2022 23:24:32 : URI: p=2&pwr=0&noipcheck
30-12-2022 23:28:43 : URI: p=2&pwr=1110&noipcheck&debug
30-12-2022 23:28:51 : URI: p=2&pwr=0&noipcheck&debug
30-12-2022 23:31:57 : URI: p=2&pwr=2.200000
30-12-2022 23:50:02 : URI: p=2&pwr=0
31-12-2022 06:59:35 : URI: p=2&pwr=3
31-12-2022 07:04:35 : URI: p=2&pwr=0
31-12-2022 10:39:31 : URI: p=2&pwr=2.900000
31-12-2022 10:53:31 : URI: p=2&pwr=1507.900000
31-12-2022 10:53:36 : URI: p=2&pwr=1326.400000
31-12-2022 10:53:41 : URI: p=2&pwr=1410.200000
31-12-2022 10:53:46 : URI: p=2&pwr=2.900000

How can I now let write a script f.e.

simulate <hostname> <mydate> <starttimestamp> <endtimestamp> <logfilename>

where the date are filtered for <mydate> <starttimestamp> <endtimestamp> and the URI should be called with <hostname>.

I have problems in making the sleep as the second line needs to be read in advanced, so that I know the time difference.

f.e. simulate https://www.xxxx.at 31-12-2022 10:00:00 10:55:00 log.txt

How can this be done in bash under unix?

It should then call https://www.xxxx.at?<URI> but with the timedelay after the nearest <starttimestamp> between each call according the logtimes but now.

f.e:

     now: 
     `curl` "https://www.xxxx.at?p=2&pwr=2.900000"
     `sleep` timedifference [31-12-2022 10:53:31 - 31-12-2022 10:39:31]
     `curl` "https://www.xxxx.at?p=2&pwr=1507.900000"
  ...

 `sleep` timedifference [31-12-2022 10:53:46 - 31-12-2022 10:53:41] 
 `curl` &quot;https://www.xxxx.at?p=2&amp;pwr=2.900000&quot;

HHUber
  • 3
  • 6
    Why the obsession with unreadable one liners? You're trying to get something to work, so take the time and effort to have it readable and understandable! (Optimisation should always be the last step - if it's considered at all - there's zero point having a beautifully optimised piece of code that doesn't work.) – Chris Davies Dec 31 '22 at 16:55
  • 1
    Is this code to run on UNIX or Linux? Specifically do you have a version of date that can parse date/time strings and convert them to seconds? – Chris Davies Dec 31 '22 at 16:57
  • 4
    oneliners are the kiss of death for legible software. Posting one and asking for help with it is like cooking a meal and putting it through a blender before asking someone to taste it for feedback. – Ed Morton Dec 31 '22 at 18:32
  • 1
    See also the previous advice you got about oneliners. – Ed Morton Dec 31 '22 at 19:01
  • And this related answer from a range of responses about one-liners – Chris Davies Jan 01 '23 at 00:33

1 Answers1

0

I could now do it in one awk script [named mysim.awk]:

    BEGIN { FS="- :" }
        {
        split($1,a,"[: -]")
        secs = mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6])
        cmd[NR] = a[11]
        t[NR] = a[6] + 60 * (a[5] + 60 * a[4])
        dt[NR] =a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]
        }
    END {
        split(sdt,b,"[-: ]")
        split(edt,bb,"[-: ]")
        startsecs = mktime(b[3]" "b[2]" "b[1]" "b[4]" "b[5]" "b[6])
        endsecs   = mktime(bb[3]" "bb[2]" "bb[1]" "bb[4]" "bb[5]" "bb[6])
        print sdt"[->"startsecs"] bis "edt"[->"endsecs"]"
        for (c = 1; c <= NR-1; c++)
            {
            if ((mktime(dt[c]) >= startsecs) && (mktime(dt[c]) <= endsecs))
                {
                d = t[c+1] - t[c]
                if (d < 0) d += 86400
                h = d / 3600
                d %= 3600
                m = d / 60
                s = d % 60
                printf "sleep %02d ; %s%s \n",s,"curl \"https://xxxxx.at/sendsms.php?noresponse&noipcheck&nosms&debug&",cmd[c]"\""
                }
            }
        }

call from LINUX prompt with startdatetime and enddatetime f.e:

awk -v sdt="31-12-2022 11:00:00" -v edt="31-12-2022 13:00:00" -f mysim.awk mylog.txt

or by a bash script (named runsimulate.sh] like this:

  #!/bin/bash
  # call f.e.  ./runsimulate.sh "02-01-2023 14:00:00" "02-01-2023 14:50:00" <logfilename>
  awk -f mysim.awk -v sdt="$1" -v edt="$2" $3