How to Extract logs between 2 timestamps in Unix - unix

I need to extract the logs between two timestamps from a file in Unix. I basically kind of need those logs to be copied and output in a different file so that I can copy them.
Is there an efficient way to do this? The log format looks like this - The timestamp is in a separate line from the actual logs.
Tue 21 Apr 14:00:00 GMT 2020
{"items":[{"cpu.load": "0.94","total.memory": "6039.798 MB","free.memory": "4367.152 MB","used.memory": "1672.646 MB","total.physical.system.memory": "16.656 GB","total.free.physical.system.memory": "3860.197 MB","total.used.physical.system.memory": "12.796 GB","number.of.cpus": "8"}]}
Tue 21 Apr 18:00:00 GMT 2020
{"items":[{"cpu.load": "0.76","total.memory": "6039.798 MB","free.memory": "4352.656 MB","used.memory": "1687.142 MB","total.physical.system.memory": "16.656 GB","total.free.physical.system.memory": "3858.203 MB","total.used.physical.system.memory": "12.798 GB","number.of.cpus": "8"}]}
I am doing this but it only prints out the timestamp and not the actual logs
cat file.txt | awk -F, '{ if ($1>"Fri 21 Aug 14:00:00 GMT 2020" && $1<"Sat 22 Aug 18:00:00 GMT 2020") print }'
Can someone advice.

Related

Formatting bootstrap-datepicker result

So here is how i setup my datepicker
$('#picker').datepicker({
format: 'yyyy-mm-dd',
multidate: true
});
and then here is how i showing the result
$('#picker').on('changeDate', function(event) {
console.log( $('#picker').datepicker("getDates"));
});
here is the result from console
[Tue Jun 02 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 10 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 17 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat)]
How can i make it to yyyy-mm-dd format ?
So here is how i fix it
$('#picker').datepicker("getFormattedDate")
maybe it will help others
Give moment.js a try. It is a very powerful tool not just only to format Date objects, but to manipulate them and make custom validations.

Convert string Fri, 04 Aug 2017 13:07:44 into 04-08-2017 in unix

I would like to convert the date " Fri, 04 Aug 2017 13:07:44 GMT " into 04-08-2017 .
And code should recursively convert all string format into specified format.
You may use the -d option with date command.
Execute this in your command line to get the desired output.
date -d 'Fri, 04 Aug 2017 13:07:44 GMT' +'%d-%m-%Y'
Edit :
If you want to populate it in a script you may use something like this:
#!/bin/bash
input_date="Fri, 04 Aug 2017 13:07:44 GMT"
output_date=$(date -d "$input_date" +'%d-%m-%Y')
echo $output_date

Changing a string to datetime format in where clause for comparison

I have a dataset that looks like this:
datetime count
18:28:20.602 UTC DEC 08 2016 1
20:42:32.017 UTC DEC 08 2016 5
15:33:40.691 UTC DEC 08 2016 1
17:11:54.008 UTC DEC 08 2016 3
20:28:57.861 UTC DEC 08 2016 0
.
.
.
.
The datetime column is in the string format. I'm having difficulty in converting it to a timestamp.
How do I write a Impala/Hive query so that I get the data between '18:28:00.000 UTC DEC 08 2016' to '18:33:00.000 UTC DEC 08 2016'
With Hive:
cast(from_unixtime(unix_timestamp(SHITTY_FORMAT, 'HH:mm:ss.SSS zzz MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, then into String standard format (in local timezone because that's the Hive convention), then into a Timestamp.
There is no easier way, unfortunately. And you may have some edge cases because of the 1h overlap in summer/winter times.
Source: the Hive documentation, of course...
With Impala (which does not support the zzz format modifier):
cast(from_unixtime(unix_timestamp(regexp_replace(SHITTY_FORMAT, ' UTC ', ' '), 'HH:mm:ss.SSS MMM dd yyyy'), 'yyyy-MM-dd HH:mm:ss.SSS') as Timestamp)
...will translate your shitty String format into a UNIX timestamp, assuming that all your inputs are in UTC, then into String standard format (in UTC timezone because that's the Impala convention), then into a Timestamp.

Moment js timezone off by 1 hour

I can't figure out what I am doing wrong here.
Passing in a string to moment, with the format and calling .toDate().
toDate() ends up returning a time that is off by 1 hour
moment("2015-11-19T18:34:00-07:00", "YYYY-MM-DDTHH:mm:ssZ").toDate()
> Thu Nov 19 2015 17:34:00 GMT-0800 (PST)
The time should be 18:34, not 17:34. The timezone is showing -08, when it should be showing -07

Time zone and BST woes in Europe/London

I am really struggling with my hwclock since the UK changed to British Summer Time (BST) last weekend on my Centos 5.8 KVM guest.
Here's some command outputs..
[root#host ~]# TZ=Europe/London date
Mon Apr 2 08:12:03 EDT 2012
[root#host ~]# TZ=Europe/Jersey date
Mon Apr 2 08:12:11 EDT 2012
[root#host ~]# TZ=Europe/Paris date
Mon Apr 2 14:12:16 CEST 2012
[root#host ~]# TZ=Europe/Rome date
Mon Apr 2 14:12:22 CEST 2012
[root#host ~]# TZ=Europe/Athens date
Mon Apr 2 15:12:27 EEST 2012
[root#host ~]# hwclock -rD
hwclock from util-linux-2.13-pre7
Using /dev/rtc interface to clock.
Last drift adjustment done at 1333367010 seconds after 1969
Last calibration done at 1333367010 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2012/04/02 12:22:27
Hw clock time : 2012/04/02 12:22:27 = 1333369347 seconds since 1969
Mon 02 Apr 2012 12:22:27 PM UTC -0.422061 seconds
And finally when I ln -sf to Europe/London it just goes back to EDT :(
[root#host ~]# ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime
[root#host ~]# date
Mon Apr 2 08:23:02 EDT 2012
:(
Here's also some contents of files:
[root#host ~]# cat /etc/sysconfig/clock
ZONE="Europe/London"
UTC=true
ARC=false
Really struggling here guy and have googled til my eyes popped out but to no avail.
smybolically linking the files doesn't work - they need to be physically copied
[root#xxx]# mv /etc/localtime /etc/localtime.default
[root#xxx]# cp /usr/share/zoneinfo/Europe/London /etc/localtime
[root#xxx]# date
Fri Apr 13 11:35:57 BST 2012
Hope that helps :-)
I had a similar problem on my mailserver runninng CentOS 6.4 64-bit. Mail coming in marked 6 hours forward in time! I set Oslo-time for the server. The clock was correct but it showed the wrong time zone. It should be CEST:
/etc/localtime -> /usr/share/zoneinfo/Europe/Oslo
$date
Thu Sep 19 17:08:42 EDT 2013
Cities in the same time zone showed CEST. Strange. The only way to fix it was to use Copenhagen instead.
localtime -> /usr/share/zoneinfo/Europe/Copenhagen
$ date
Thu Sep 19 17:16:55 CEST 2013
So if nothing works try to use a city in the same time zone.

Resources