epoch time comparison bsd / MacOSX - macos

This should be obvious to all but me,
I have a file full of epoch timestamps and I'm trying to get the records from the last 7 days.
So I tried this in my mac:
PAST=$(gdate -d "7 days ago" +%s)
if [ $timeval -gt $PAST ]; then
do stuff
fi
Example erroneous output:
TS from data, Human fr data, TS from $PAST, Human from $PAST
1419981977690,Tue Jun 9 06 14 50 PST 46967,1421129827,Mon Jan 12 22 17 07 PST 2015
1419400311440,Thu Jan 2 00 17 20 PST 46949,1421129827,Mon Jan 12 22 17 07 PST 2015
1420341489480,Fri Oct 30 06 38 00 PST 46978,1421129827,Mon Jan 12 22 17 07 PST 2015
1421232275040,Thu Jan 22 07 04 00 PST 47007,1421129827,Mon Jan 12 22 17 07 PST 2015

The timestamps in your file are in milliseconds, not seconds. Divide them by 1000.

Related

Cron Job in Spring Boot

I want to automate a task quarterly in a year.
Task should execute
Jan 1st
April 1st
July 1st
October 1st
etc
As I tried #Scheduled(cron = "0 0 6 1 1/3 ?") in Spring Boot application but its not working currently and mail didn't trigger quarterly.
Try this
#scheduled(cron = "0 0 6 1 */3 *")
So read the first day of each 3 month at 6:00
Try this one -
#scheduled(cron ="0 0 0 1 JAN,APR,JUL,OCT ? *")
Next execution:
Wed Jul 01 00:00:00 UTC 2020
Thu Oct 01 00:00:00 UTC 2020
Fri Jan 01 00:00:00 UTC 2021
Thu Apr 01 00:00:00 UTC 2021
Thu Jul 01 00:00:00 UTC 2021
Fri Oct 01 00:00:00 UTC 2021
Sat Jan 01 00:00:00 UTC 2022
Fri Apr 01 00:00:00 UTC 2022
Fri Jul 01 00:00:00 UTC 2022
Sat Oct 01 00:00:00 UTC 2022

Apply set of operation on one entire column in unix file

I have a file looks like :
"JOB1 #43", 43 "SUCCESS", 1479079800029
"JOB1 #42", 42 "SUCCESS", 1478993400042
"JOB1 #41", 41 "SUCCESS", 1478907000065
"JOB1 #40", 40 "SUCCESS", 1478820600085
"JOB1 #39", 39 "SUCCESS", 1478734200051
need to change last column (timestamp) with date and time format.
Tried so far :
for i in `cat file | awk '{print $NF}'`
do
date -d #$( echo "(${i} + 500) / 1000" | bc)
done
Output:
Sun Nov 13 23:30:00 GMT 2016
Sat Nov 12 23:30:00 GMT 2016
Fri Nov 11 23:30:00 GMT 2016
Thu Nov 10 23:30:00 GMT 2016
Wed Nov 9 23:30:00 GMT 2016
Expected output is
"JOB1 #43", 43 "SUCCESS", Sun Nov 13 23:30:00 GMT 2016
"JOB1 #42", 42 "SUCCESS", Sat Nov 12 23:30:00 GMT 2016
"JOB1 #41", 41 "SUCCESS", Fri Nov 11 23:30:00 GMT 2016
"JOB1 #40", 40 "SUCCESS", Thu Nov 10 23:30:00 GMT 2016
"JOB1 #39", 39 "SUCCESS", Wed Nov 9 23:30:00 GMT 2016
Can we change it in file itself?
Any help would be appreciated.
Try:
awk -F',\\s*' '{print $1 "," $2 "," strftime("%c", $3/1000)}' file
Outputs:
"JOB1 #43",43 "SUCCESS",Sun 13 Nov 2016 23:30:00 GMT
"JOB1 #42",42 "SUCCESS",Sat 12 Nov 2016 23:30:00 GMT
"JOB1 #41",41 "SUCCESS",Fri 11 Nov 2016 23:30:00 GMT
"JOB1 #40",40 "SUCCESS",Thu 10 Nov 2016 23:30:00 GMT
"JOB1 #39",39 "SUCCESS",Wed 09 Nov 2016 23:30:00 GMT
You may like to adjust the date format as necessary.
You can use this awk command (will work with non-gnu awk as well):
awk -F , '{cmd="TZ=UTC date -d #" sprintf("%d", ($NF+500)/1000);
cmd | getline dt; gsub(/[0-9]+$/, dt); close(cmd)} 1' file
"JOB1 #43", 43 "SUCCESS", Sun Nov 13 23:30:00 UTC 2016
"JOB1 #42", 42 "SUCCESS", Sat Nov 12 23:30:00 UTC 2016
"JOB1 #41", 41 "SUCCESS", Fri Nov 11 23:30:00 UTC 2016
"JOB1 #40", 40 "SUCCESS", Thu Nov 10 23:30:00 UTC 2016
"JOB1 #39", 39 "SUCCESS", Wed Nov 9 23:30:00 UTC 2016
We use getline function to call your desired date command and store output in variable dt
Finally wee use gsub instead of assignment i.e. $NF = dt to maintain formatting of input record in output
As you already got the content to replace with, the merging can easily be done using python script.
f1=open("a.txt")
f2=open("b.txt")
lines1 = f1.readlines()
lines2 = f2.readlines()
output_lines=[]
for (l1,l2) in zip(lines1,lines2):
fields = l1.split(",")
fields[2] = l2
output_line = ",".join(fields)
output_lines.append(output_line)
f3 = open("result.txt","w")
f3.writelines(output_lines)
f3.close()
output : result.txt
"JOB1 #43", 43 "SUCCESS",Sun Nov 13 23:30:00 GMT 2016
"JOB1 #42", 42 "SUCCESS",Sat Nov 12 23:30:00 GMT 2016
"JOB1 #41", 41 "SUCCESS",Fri Nov 11 23:30:00 GMT 2016
"JOB1 #40", 40 "SUCCESS",Thu Nov 10 23:30:00 GMT 2016
"JOB1 #39", 39 "SUCCESS",Wed Nov 9 23:30:00 GMT 2016

Crontab schedule every two week

There is a question happened this week on my crontab job.
It's be set as below and works normal every two weeks until now.
10 06 * * 1 test $(($(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
The problem is
$(($(date +\%W)\%2)) would be 08, over 7 in February.
And it will show error message if you run in bash: value too great for base (error token is "08").
There is also not working when I try to revise it for forcing decimal base purpose :
10 06 * * 1 test $((10#$(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
Does someone know how to solve this issue? Many thanks.
Every two week at midnight
0 0 */15 * * echo 'test' > tmp.txt
Your cron job will be run at: (5 times displayed)
2016-03-15 00:00:00 UTC
2016-03-30 00:00:00 UTC
2016-04-15 00:00:00 UTC
2016-04-30 00:00:00 UTC
2016-05-15 00:00:00 UTC
Make condition in your script
The use of % sign is discouraged in crontab!
To ensure having jod started on monday, every two weeks you've better to create a small script.
#!/bin/bash
if [ "$1" == "-f" ] ;then # use "-f" switch to force execution on odd weeks
shift
else
printf -v d '%(%W)T' -1
((10#$d%2)) || exit 0 # stop here if odd week
fi
printf "Real job begin at %(%c)T, there...\n" -1
This could be shortened to one single line:
#!/bin/bash
[ "$1" == "-f" ]&&shift||{ printf -v d '%(%W)T' -1;((10#$d%2))||exit;}
So you could add standard lines in your crontab:
10 06 * * 1 path/to/myScript > /tmp/test.log
Sample: year 2016
date -d 2016-1-4\ 10:06 +%s
1451898360
for i in {0..52};do
printf -v d '%(%W)T' $((c=i*7*86400+1451898360))
((10#$d%2)) && printf "%(%a %d %b, week: %W)T\n" $c
done | cat -n
1 Mon 04 Jan, week: 01
2 Mon 18 Jan, week: 03
3 Mon 01 Feb, week: 05
4 Mon 15 Feb, week: 07
5 Mon 29 Feb, week: 09
6 Mon 14 Mar, week: 11
7 Mon 28 Mar, week: 13
8 Mon 11 Apr, week: 15
9 Mon 25 Apr, week: 17
10 Mon 09 May, week: 19
11 Mon 23 May, week: 21
12 Mon 06 Jun, week: 23
13 Mon 20 Jun, week: 25
14 Mon 04 Jul, week: 27
15 Mon 18 Jul, week: 29
16 Mon 01 Aug, week: 31
17 Mon 15 Aug, week: 33
18 Mon 29 Aug, week: 35
19 Mon 12 Sep, week: 37
20 Mon 26 Sep, week: 39
21 Mon 10 Oct, week: 41
22 Mon 24 Oct, week: 43
23 Mon 07 Nov, week: 45
24 Mon 21 Nov, week: 47
25 Mon 05 Dec, week: 49
26 Mon 19 Dec, week: 51
27 Mon 02 Jan, week: 01
For comparission with idea of using 15th and 30th each months:
for i in {1..12}/{15,30};do
date -d '2016/'$i +'%a %d %b, week: %W' 2>/dev/null
done | cat -n
1 Fri 15 Jan, week: 02
2 Sat 30 Jan, week: 04
3 Mon 15 Feb, week: 07
4 Tue 15 Mar, week: 11
5 Wed 30 Mar, week: 13
6 Fri 15 Apr, week: 15
7 Sat 30 Apr, week: 17
8 Sun 15 May, week: 19
9 Mon 30 May, week: 22
10 Wed 15 Jun, week: 24
11 Thu 30 Jun, week: 26
12 Fri 15 Jul, week: 28
13 Sat 30 Jul, week: 30
14 Mon 15 Aug, week: 33
15 Tue 30 Aug, week: 35
16 Thu 15 Sep, week: 37
17 Fri 30 Sep, week: 39
18 Sat 15 Oct, week: 41
19 Sun 30 Oct, week: 43
20 Tue 15 Nov, week: 46
21 Wed 30 Nov, week: 48
22 Thu 15 Dec, week: 50
23 Fri 30 Dec, week: 52
As you could see, there miss one operation on weeks: 6, 9, 10, 21, 32 and 45. At least there is 3 operation less in one year.

How to display database values once in view page using laravel

This is my table wys_attendence:
id studid adate amonth ayear acls_id attendence
1 28 02 07 2015 10 1
2 31 02 07 2015 10 0
4 32 02 07 2015 10 1
5 28 13 07 2015 10 0
6 31 13 07 2015 10 1
7 32 13 07 2015 10 1
9 28 14 07 2015 10 1
10 31 14 07 2015 10 1
11 32 14 07 2015 10 1
13 28 15 07 2015 10 1
14 31 15 07 2015 10 0
15 32 15 07 2015 10 1
17 28 16 07 2015 10 0
18 31 16 07 2015 10 1
19 32 16 07 2015 10 1
21 28 17 07 2015 10 1
22 31 17 07 2015 10 1
23 32 17 07 2015 10 0
24 28 20 08 2015 10 1
25 31 20 08 2015 10 1
26 32 20 08 2015 10 0
I want to check if every day of a specific year and month is in the table, and to display all the dates of a selected month and year.
if I am select july 2015.
The output I get is incorrect. This is what I get:
studid 02 02 02 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17
But I want it like this:
studid 2 13 14 15 16 17
my controller code is here
`$amonth = Input::get('amonth');
$ayear = Input::get('ayear');
$teacher_attend=WysTeacherattendance::all();
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->get();`
my view.blade.php code is here
<th>studid</th>
#foreach($attendance as $attendances)
<th>{{$attendances->t_adate}}</th>
#endforeach
How can I modify my query to achieve the above result as well as check if days of my selected month and year are in the database or not if in database then day display only once.?
If you want to group records by date then use groupBy() as explained on http://laravel.com/docs/4.2/queries#selects
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->groupBy('t_adate')
->get();

extract data date wise and do average calculation

How to extract data date wise and do average calculation per date from the below shown output. last column is average.
Sun Jul 5 00:00:02 IST 2015, 97
Sun Jul 5 00:02:01 IST 2015, 97
Sun Jul 5 00:04:02 IST 2015, 97
Mon Jul 6 00:00:01 IST 2015, 73
Mon Jul 6 00:02:02 IST 2015, 93
Mon Jul 6 00:04:02 IST 2015, 97
Tue Jul 7 00:00:02 IST 2015, 97
Tue Jul 7 00:02:02 IST 2015, 97
Tue Jul 7 00:04:01 IST 2015, 97
Wed Jul 8 00:00:01 IST 2015, 98
Wed Jul 8 00:02:02 IST 2015, 98
Wed Jul 8 00:04:01 IST 2015, 98
Thu Jul 9 00:00:02 IST 2015, 100
Thu Jul 9 00:02:01 IST 2015, 100
Thu Jul 9 00:04:01 IST 2015, 100
Fri Jul 10 00:00:01 IST 2015, 100
Fri Jul 10 00:02:02 IST 2015, 100
Fri Jul 10 00:04:02 IST 2015, 100
Sat Jul 11 00:00:01 IST 2015, 73
Sat Jul 11 00:02:01 IST 2015, 73
Sat Jul 11 00:04:02 IST 2015, 73
want output as
Jun 6 - 97
Jun 7 - 86.66
...
You can use this awk:
awk -F ', ' '{
split($1, a, " ");
k=a[2] OFS a[3];
if(!(k in c))
b[++n]=k;
c[k]++;
sum[k]+=$2
}
END{
for(i=1; i<=n; i++)
printf "%s - %.2f\n", b[i], (sum[b[i]]/c[b[i]])
}' file
Jul 5 - 97.00
Jul 6 - 87.67
Jul 7 - 97.00
Jul 8 - 98.00
Jul 9 - 100.00
Jul 10 - 100.00
Jul 11 - 73.00

Resources