Thursday, June 27, 2013

Unix: System date and time to a variable, and using that variable



Caveat: I am a Unix novice.  There may be better ways to do this, but this worked for me.

Show system date and time in the default format:
$ date
Thu Jun 27 21:23:36 CDT 2013

Show system date and time in yymmdd_hhmmss format (two digit year):
$ date "+%y%m%d_%H%M%S"
130627_212355

Show system date and time in yyyymmdd_hhmmss format (four digit year):
$ date "+%Y%m%d_%H%M%S"
20130627_212416

Save the system date and time in yyyymmdd_hhmmss format to a variable, then show it:
$ var="$(date '+%Y%m%d_%H%M%S')"
$ echo datetime is $var
datetime is 20130627_212437

My requirements: Schedule a SAS program (pd.sas) to run at 4am on June 28th.  Send the SAS log to a file whose name includes the system date and time so we can archive the logs.

Step 1: Create a (script) file called go
#!/bin/bash
ts="$(date '+%Y%m%d_%H%M%S')"
echo "sas pd.sas -log pd_$ts.log" | at 0400 Jun 28
Step 2: Execute a (script) file called go
$ . go

Monday, June 24, 2013

Python: Reading a JSON dataset

I just completed an online Python course.  I have been experimenting and there is much about this language to love!

Here is a simple JSON dataset, called min_max_json.txt:

{
"min": 34,
"max": 82
}


Here is the source code, called read_min_max_json.py, which reads the JSON dataset and calculates the range (defined as max - min):

import json
data = json.loads(open('min_max_json.txt').read())
print("This is what was read in:")
print(data)
min = data["min"]
max = data["max"]
range = max - min
print("The range is " + str(range) + ".")


Here is the output:

This is what was read in:
{'min': 34, 'max': 82}
The range is 48.


Note: Using Python 3.3.2 with IDLE.