Pattern Matching in cron - regex

I want to grep line which begin with # or #* or number or #number. In other word, I want to greping from the beginning of cron command until end of line. I don't need crontab header.
Here is crontab file :
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#*/56 * * * * root bash /media/data/minutely.sh
#20 * * * * root bash /media/data/hourly.sh
#* * * * * root bash /media/data/looping.sh
* * * * * root bash /media/data/looping2.sh
20 * * * * root bash /media/data/hourly2.sh
And what I want is :
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#*/56 * * * * root bash /media/data/minutely.sh
#20 * * * * root bash /media/data/hourly.sh
#* * * * * root bash /media/data/looping.sh
* * * * * root bash /media/data/looping2.sh
20 * * * * root bash /media/data/hourly2.sh
.
.
.
.
#until end of line
I have try with :
cat /etc/crontab | grep '^[^#0-9]'
But it give me output :
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
How can grep only pattern that I have explain above?
Sorry for bad english.

Here's another sed variant that starts printing at the first line that begins with a digit:
sed -n '/^[0-9]/,$p' /etc/crontab

You can do it with sed:
sed -n '6,${/^[0-9#]/p}' /etc/crontab
-n option prevents to display lines automatically
6,$ is a range of lines (from line 6 to the last line)
/^[0-9#]/ tests if the line begin with # or a digit. If the test succeeds, p print the line.

Related

How to download data from multiple folders over https using python

I'm trying to download files from each folder from
https://nomads.ncdc.noaa.gov/data/gfs-avn-hi/201705/. From each folder I would only need the 0000_000.grb2 files with the last 3 numbers increasing by 12 up to 384. For example, the first file would be 0000_000,grb2 -> 0000_012.grb2 -> etc. After, the program would close and move on to the next folder. I'm still very new to python and programming in general. This is what I have at the moment. It's a little messy. Would like to get this to work, so I don't have to manually download the data:
import urllib
core = 'https://nomads.ncdc.noaa.gov/data/gfs-avn-hi/201704/'
year = 2017
month = 05
day = 0
forecast_hour = 0
for i in range (0,31):
day_fixed = str(day).zfill(2)
day += 1
for i in range (0,384):
forecast_hour_fixed = str(forecast_hour).zfill(3)
forecast_hour += 12
urllib.urlretrieve(core + "/" + year + month + day_fixed + "/" + year + month + day_fixed + "gfs_3_201705" + day_fixed + "_0000_" + forecast_hour_fixed +".grb2", "gfs_3_201705" + day_fixed + "_0000_" + forecast_hour_fixed + ".grb2")

Modulus in Pascal

I am trying to translate some Pascal code into C++ code. I am stuck trying to figure out how to translate this portion.
Function ThetaG_JD(jd : double) : double;
var
UT,TU,GMST : double;
begin
**UT := Frac(jd + 0.5);**
jd := jd - UT;
TU := (jd - 2451545.0)/36525;
GMST := 24110.54841 + TU * (8640184.812866 + TU * (0.093104 - TU * 6.2E-6));
**GMST := Modulus(GMST + 86400.0*1.00273790934*UT,86400.0);**
ThetaG_JD := twopi * GMST/86400.0;
end; {Function ThetaG_JD}
I am particularly having trouble with the two lines I made bold. How can I translate this to c++? Thank you so much.
In C++ the equivalent functions would be:
fmod to get a floating point modulus
modf to break a floating point item into its fraction and integral parts (equivalent of Frac).
If you want to calculate Julian Day, Greenwich Mean Sidereal Time and Local Mean Sidereal Time, maybe the below can help you - written in PowerShell:
<#
.Synopsis
Astronomy calculations
.Description
Some helper functions to calculate:
- Julian Day,
- Greenwich Mean Sidereal time,
- Local Mean Sidereal Time.
#>
cls
# https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
function Get-JulianDay
{
param ( [System.DateTime]$dt )
$year = $dt.Year
$month = $dt.Month
$day = $dt.Day
$hour = $dt.Hour
$minute = $dt.Minute
$second = $dt.Second
$a = [System.Math]::Floor((14 - $month) / 12)
$y = $year + 4800 - $a
$m = $month + 12 * $a - 3
$JDN = $day + [System.Math]::Floor((153 * $m + 2) / 5) + 365 * $y + [System.Math]::Floor($y / 4) - [System.Math]::Floor($y / 100) + [System.Math]::Floor($y / 400) - 32045
$JD = $JDN + ($hour - 12) / 24 + $minute / 1440 + $second / 86400
return ($JD)
}
# https://en.wikipedia.org/wiki/Sidereal_time#Definition
# http://aa.usno.navy.mil/faq/docs/GAST.php
function Get-GMST
{
param ( [double]$JD )
$D = $JD - 2451545.0
$GMST = 18.697374558 + 24.06570982441908 * $D
return ($GMST % 24)
}
function Get-LMST
{
param ( [double]$gmst, [double]$longitude )
return ( $gmst + $longitude / 15.0 )
}
# Test above functions
$current = (Get-Date).ToUniversalTime()
$jd = Get-JulianDay -dt $current
$gmst = Get-GMST -JD $jd
$longitude = 17.668487800
$lmst = Get-LMST -gmst $gmst -longitude $longitude
$lst = [timespan]::FromHours($lmst).ToString()
Write-Host "Local mean sidereal time: $lst"

BATCH - RPS - Rock, Paper, Scissors - Keep failing after a few rounds

I have been trying to make a rock, paper, scissors game in batch. So far, the script runs and I can make it play against itself or to play against me (I need to change the B var from /A and random number to /P and have user input).
The thing is that after a few (different amount every time) rounds, the script crash and I do not have time to read the last line of text (which would help me find the error). I have tried with ECHO ON and OFF, I also tried to run 'RPS.bat >> log.txt' from a different DOS window (with the right directory chosen). That did log everything until it crashed, and did not get the last line either.
I have been looking through it several times, but I cant find the error... :(
I made the same game in PHP with currently no errors... (it is a totally different language, I know)
So here is my script:
#ECHO OFF
:TOP
SET /A AWON=0
SET /A BWON=0
SET /A NWON=0
:GAME
SET /A A=%RANDOM% %% 3 + 1
SET /P B=Number from 1 to 3
IF %A% EQU 1 (
GOTO A1
) ELSE IF %A% EQU 2 (
GOTO A2
) ELSE IF %A% EQU 3 (
GOTO A3
) ELSE (
GOTO GAME
)
:A1
IF %B% EQU 1 (
GOTO DRAW
) ELSE IF %B% EQU 2 (
GOTO BWIN
) ELSE IF %B% EQU 3 (
GOTO AWIN
) ELSE (
GOTO GAME
)
:A2
IF %B% EQU 1 (
GOTO ARAW
) ELSE IF %B% EQU 2 (
GOTO DRAW
) ELSE IF %B% EQU 3 (
GOTO BWIN
) ELSE (
GOTO GAME
)
:A3
IF %B% EQU 1 (
GOTO BWIN
) ELSE IF %B% EQU 2 (
GOTO AWIN
) ELSE IF %B% EQU 3 (
GOTO DRAW
) ELSE (
GOTO GAME
)
:AWIN
SET /A AWON=%AWON% + 1
ECHO -------------------------------------------------------------------------------
ECHO A won this round!
ECHO ---
ECHO A won: %AWON%
ECHO B won: %BWON%
ECHO Draw: %NWON%
PAUSE
GOTO GAME
:BWIN
SET /A BWON=%BWON% + 1
ECHO -------------------------------------------------------------------------------
ECHO B won this round!
ECHO ---
ECHO A won: %AWON%
ECHO B won: %BWON%
ECHO Draw: %NWON%
PAUSE
GOTO GAME
:DRAW
SET /A NWON=%NWON% + 1
ECHO -------------------------------------------------------------------------------
ECHO This round was a draw!
ECHO ---
ECHO A won: %AWON%
ECHO B won: %BWON%
ECHO Draw: %NWON%
PAUSE
GOTO GAME
Thanks to Gray for helping me out with my stupid mistake ^^
Also thanks for the script, but if I am not missing something here, it is not quite correct.
#ECHO OFF
:GAME
SET /P A=A:
SET /P B=B:
SET /A WINNER=(%A%-%B%) %% 3
ECHO %A% - %B% = %WINNER%
IF %WINNER% EQU 1 (
echo A WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo B WON!
))
PAUSE
GOTO GAME
Run that script (a 'shortened' version of Gray's script). Use the variables like as in my 'table', you can see that by choosing 1 (rock), A is not able to win (A=1 vs B=3 should be: A WON). Do you have any idea on how to fix this? I guess a 'hard-coded?' solution when A=1 and B=3 would be best? (I mean that when those are the values, the 'normal' script does not apply, but a special code gives the result instead).
In my table, if the last sign is '+', then the script gives the correct answer, if the last sign is a '-', then the script gives an incorrect answer.
ITEM VALUE
ROCK 1
PAPER 2
SCISSORS 3
A - B = WINNER
1 - 1 = 0 DRAW +
1 - 2 = -1 B WON +
1 - 3 = -2 A WON -
2 - 1 = 1 A WON +
2 - 2 = 0 DRAW +
2 - 3 = -1 B WON +
3 - 1 = 2 B WON +
3 - 2 = 1 A WON +
3 - 3 = 0 DRAW +
Also, if you have any tips on how to keep the script more tidy and use less lines code to do the same job, I would gladly listen!
Thanks in advance! - Espen
Your error likely says:
The system cannot find the batch label specified - ARAW
This is because you have GOTO ARAW in your A2 label, when it looks like you meant to do GOTO AWIN
Bonus: as a more "clever" way of determining the winner (can surely be improved more)
#echo off
rem rock:1; paper:2; scissors:3
:GAME
SET /A CPU=%RANDOM% %% 3 + 1
SET /P PLAYER=Number from 1 to 3
cls
echo %CPU% vs %PLAYER%
rem set /A WINNER=(%CPU%-%PLAYER%) %% 3 //this does not work with batch - my mistake
SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3
IF %WINNER% EQU 1 (
echo CPU WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo YOU WON!
))
GOTO GAME
And finally, here is one that prints "rock", "paper", and "scissors" to make it a little more... fun
#echo off
rem rock:1; paper:2; scissors:3
:GAME
SET /A CPU=%RANDOM% %% 3 + 1
SET /P PLAYER=Number from 1 to 3
goto CPUTYPE
:TYPE1
goto PLAYERTYPE
:TYPE2
cls
echo CPU:%CPUT% vs YOU:%PLAYERT%
rem set /A WINNER=(%CPU%-%PLAYER%) %% 3 //this does not work with batch - my mistake
SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3
IF %WINNER% EQU 1 (
echo CPU WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo YOU WON!
))
GOTO GAME
:CPUTYPE
IF %CPU% EQU 1 (
SET CPUT="ROCK"
) ELSE (
IF %CPU% EQU 2 (
SET CPUT="PAPER"
) ELSE (
SET CPUT="SCISSORS"
))
GOTO TYPE1
:PLAYERTYPE
IF %PLAYER% EQU 1 (
SET PLAYERT="ROCK"
) ELSE (
IF %PLAYER% EQU 2 (
SET PLAYERT="PAPER"
) ELSE (
SET PLAYERT="SCISSORS"
))
GOTO TYPE2
Updated version based on asker's PvP version (fixed modul
#ECHO OFF
:GAME
SET /P A=A:
SET /P B=B:
SET /A WINNER=(%A% - %B% + 3) %% 3
ECHO W= %WINNER%
IF %WINNER% EQU 1 (
echo A WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo B WON!
))
PAUSE
GOTO GAME
Explanation of SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3:
2 beats 1, 3 beats 2, and 1 beats 3. If you draw it out, it is kind of circular. We use the % or modulus operator to do that. The modulus operator basically gives you the remainder if you had divided by that number using integer division.
Here are 1 through 6 mod 3, with the answer in bold.
1 / 3 = 0 rem 1
2 / 3 = 0 rem 2
3 / 3 = 1 rem 0
4 / 3 = 1 rem 1
5 / 3 = 1 rem 2
6 / 3 = 2 rem 0
Notice how the result is always < 3 and > 0? We exploit this mechanic to mimic the circular behavior of rock paper scissors. You also may have noticed I didn't do the +3 in those examples. The +3 was added because the way Microsoft chose to implement modulus, it does weird things when you have a negative number (that was how we fixed the bug you caught). I had assumed it used a modulus like I was used to where the sign didn't change the outcome. Basically, it just shifts the operation up 3 so that it is never negative. You can read more about this kind of thing here: circular buffer. I like examples, so here is something that may help.
Ties:
1v1 -> (1 - 1 + 3) % 3 -> 3 % 3 -> 1 rem 0
2v2 -> (2 - 2 + 3) % 3 -> 3 % 3 -> 1 rem 0
3v3 -> (3 - 3 + 3) % 3 -> 3 % 3 -> 1 rem 0
player 1 win
1v3 -> (1 - 3 + 3) % 3 -> 1 % 3 -> 0 rem 1
2v1 -> (2 - 1 + 3) % 3 -> 4 % 3 -> 1 rem 1
3v2 -> (3 - 2 + 3) % 3 -> 4 % 3 -> 1 rem 1
player 2 win
1v2 -> (1 - 2 + 3) % 3 -> 2 % 3 -> 0 rem 2
2v3 -> (2 - 3 + 3) % 3 -> 2 % 3 -> 0 rem 2
3v1 -> (3 - 1 + 3) % 3 -> 5 % 3 -> 1 rem 2
The pattern here is that all of the remainders (results of the modulus operator) all match up. How convenient! Now we can just match that case to an if-statement, and we know who won every game.

Regex Negations in Vim

Question:
How do I convert var x+=1+2+3+(5+6+7) to var x += 1 + 2 + 3 + ( 5 + 6 + 7 )
Details:
Using regular expressions, something like :%s/+/\ x\ /g won't work because it will convert += to + = (amongst other problems). So instead one would use negations (negatives, nots, whatever they're called) like so :%s/\s\#!+/\ +/g, which is about as complicated a way as one can say "plus sign without an empty space before it". But now this converts something like x++ into x + +. What I need is something more complex. I need more than one constraint in the negation, and an additional constraint afterwards. Something like so, but this doesn't work :%s/[\s+]\#!+\x\#!/\ +/g
Could someone please provide the one, or possibly two regex statements which will pad out an example operator, such that I can model the rest of my rules on it/them.
Motivation:
I find beautifiers for languages like javascript or PHP don't give me full control (see here). Therefore, I am attempting to use regex to carry out the following conversions:
foo(1,2,3,4) → foo( 1, 2, 3, 4 )
var x=1*2*3 → var x = 1 * 2 * 3
var x=1%2%3 → var x = 1 % 2 % 3
var x=a&&b&&c → var x = a && b && c
var x=a&b&c → var x = a & b & c
Any feedback would also be appreciated
Thanks to the great feedback, I now have a regular expression like so to work from. I am running these two regular expressions:
:%s/\(\w\)\([+\-*\/%|&~)=]\)/\1\ \2/g
:%s/\([+\-*\/%|&~,(=]\)\(\w\)/\1\ \2/g
And it is working fairly well. Here are some results.
(1+2+3+4,1+2+3+4,1+2+3+4) --> ( 1 + 2 + 3 + 4, 1 + 2 + 3 + 4, 1 + 2 + 3 + 4 )
(1-2-3-4,1-2-3-4,1-2-3-4) --> ( 1 - 2 - 3 - 4, 1 - 2 - 3 - 4, 1 - 2 - 3 - 4 )
(1*2*3*4,1*2*3*4,1*2*3*4) --> ( 1 * 2 * 3 * 4, 1 * 2 * 3 * 4, 1 * 2 * 3 * 4 )
(1/2/3/4,1/2/3/4,1/2/3/4) --> ( 1 / 2 / 3 / 4, 1 / 2 / 3 / 4, 1 / 2 / 3 / 4 )
(1%2%3%4,1%2%3%4,1%2%3%4) --> ( 1 % 2 % 3 % 4, 1 % 2 % 3 % 4, 1 % 2 % 3 % 4 )
(1|2|3|4,1|2|3|4,1|2|3|4) --> ( 1 | 2 | 3 | 4, 1 | 2 | 3 | 4, 1 | 2 | 3 | 4 )
(1&2&3&4,1&2&3&4,1&2&3&4) --> ( 1 & 2 & 3 & 4, 1 & 2 & 3 & 4, 1 & 2 & 3 & 4 )
(1~2~3~4,1~2~3~4,1~2~3~4) --> ( 1 ~ 2 ~ 3 ~ 4, 1 ~ 2 ~ 3 ~ 4, 1 ~ 2 ~ 3 ~ 4 )
(1&&2&&3&&4,1&&2&&3&&4,1&&2&&3&&4) --> ( 1 && 2 && 3 && 4, 1 && 2 && 3 && 4, 1 && 2 && 3 && 4 )
(1||2||3||4,1||2||3||4,1||2||3||4) --> ( 1 || 2 || 3 || 4, 1 || 2 || 3 || 4, 1 || 2 || 3 || 4 )
var x=1+(2+(3+4*(965%(123/(456-789))))); --> var x = 1 +( 2 +( 3 + 4 *( 965 %( 123 /( 456 - 789 )))));
It seems to work fine for everything except nested brackets. If I fix the nested brackets problem, I will update it here.

Can't use same parameter twice in custom function

I wrote a custom DQL function. In this function I use the same parameter twice:
public function getSql(SqlWalker $sqlWalker)
{
$point1_lat = $this->point1_lat->dispatch($sqlWalker);
$point1_lon = $this->point1_lon->dispatch($sqlWalker);
$point2_lat = $this->point2_lat->dispatch($sqlWalker);
$point2_lon = $this->point2_lon->dispatch($sqlWalker);
$unitFactor = 6366.56486; // earth radius in km
return "
$unitFactor *
2 *
ASIN(
SQRT(
POWER(
SIN(($point1_lat - $point2_lat) * pi()/180/2),
2
) +
COS($point1_lat * pi()/180) *
COS($point2_lat * pi()/180) *
POWER(
SIN(($point1_lon - $point2_lon) * pi()/180/2),
2
)
)
)
";
}
This is how the query is executed:
$q = \App::get()->getEntityManager()->createQuery('
SELECT
s,
GEO_DISTANCE(
:lat,
:lng,
s.glat,
s.glng
) AS distance
FROM
\Application\Geo\Entity\Street s
');
$q->setMaxResults(10);
$q->setParameters(array(
'lat' => 52.25948,
'lng' => 6.76403,
));
$result = $q->getResult();
This however gives me the following exception:
Message: SQLSTATE[HY093]: Invalid parameter number: number of bound
variables does not match number of tokens
The following SQL is returned by getSql():
6366.56486 *
2 *
ASIN(
SQRT(
POWER(
SIN((? - g0_.glat) * pi()/180/2),
2
) +
COS(? * pi()/180) *
COS(g0_.glat * pi()/180) *
POWER(
SIN((? - g0_.glng) * pi()/180/2),
2
)
)
)
So I guess the exception is thrown because the named parameters are returned as indexed parameters. Is this a bug in doctrine or am I doing something wrong?
This is not a bug, you can only use each parameter once, as the dispatch() function puts the value on the stack once, to match one ? placeholder in the query.
As a workaround, you can call dispatch() several times:
public function getSql(SqlWalker $sqlWalker)
{
$point1_lat_a = $this->point1_lat->dispatch($sqlWalker);
$point1_lat_b = $this->point1_lat->dispatch($sqlWalker);
$point1_lon = $this->point1_lon->dispatch($sqlWalker);
$point2_lat_a = $this->point2_lat->dispatch($sqlWalker);
$point2_lat_b = $this->point2_lat->dispatch($sqlWalker);
$point2_lon = $this->point2_lon->dispatch($sqlWalker);
$unitFactor = 6366.56486; // earth radius in km
return "
$unitFactor *
2 *
ASIN(
SQRT(
POWER(
SIN(($point1_lat_a - $point2_lat_a) * pi()/180/2),
2
) +
COS($point1_lat_b * pi()/180) *
COS($point2_lat_b * pi()/180) *
POWER(
SIN(($point1_lon - $point2_lon) * pi()/180/2),
2
)
)
)
";
}