Hey

Hello world!

gron - greppable JSON from the command-line

JSON is everywhere. Sometimes it ends up as a file on my disk and I need to parse it to use some data.

jq is the best-known way for manipulating JSON from the command line. It's flexible and powerful, but I can never remember the syntax for anything beyond extracting a single field.

I recently discovered gron which makes JSON easily greppable by printing each field of a JSON object on its own line.

gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author"
json[0].commit.author = {};
json[0].commit.author.date = "2020-11-07T01:17:42Z";
json[0].commit.author.email = "[email protected]";
json[0].commit.author.name = "Tom Hudson";

A useful tool to have in the toolbox!

Add user and database to postgresql

How to create a postgres user and database.

Create postgres user

root@machine:~# su postgres
postgres@machine:~$ createuser --interactive --pwprompt

Add postgres database

root@machine:~# su postgres
postgres@machine:~$ createdb -O username databasename

Control NVIDIA Shield with Logitech Harmony and FLIRC

I wanted to control my NVIDIA Shield (2017) with my Harmony 650 IR remote control, but the shield doesn't have an IR receiver.

Purchased a FLIRC.

https://flirc.gitbooks.io/flirc-instructions/content/universal-remote-controls/logitech-harmony-remote-controls/nvidia-shield-tv.html

Problem solved! Harmony works a treat.

Run code in orgmode source blocks

With Babel you can easily execute code for a variety of languages from within orgmode.

To configure Babel for the languages I wanted to execute, I put the following in my .emacs.d/init.el config.

;; Configure babel for these languages
(org-babel-do-load-languages
  'org-babel-load-languages
  '((python . t)
    (shell . t)
    (dot . t)
    (org . t)
    (scheme . t)
    ))

;; run code blocks without prompting first
(setq org-confirm-babel-evaluate nil)

Then make a new .org file and try running a snippet in the file.

#+BEGIN_SRC python :results output
for x in range(0,3):
  print("Hello world")
#+END_SRC

Execute the code using C-c C-c (Control-c Control-c) with the cursor in the SRC block.

The results are included in the file like magic.

#+RESULTS:
: Hello world
: Hello world
: Hello world

Graphviz

It's particularly handy for Graphviz. You get a nicely interactive way to draw Graphs.

Execute the block the same way with C-c C-c and you get a link to the graph.

#+BEGIN_SRC dot :file graphviz.png

digraph architecture {
node [color=green] [shape=box]; "A"
node [color=blue] [shape=oval]; "B", "C"
"A" -> "B";
"B" -> "C";
"C" -> "A";
"C" -> "D";
"D" -> "E";
"E" -> "B";
}

#+END_SRC

#+RESULTS:
[[file:graphviz.png]]

/posts/graphviz.png

/posts/orgmode-babel-graphviz.png

References

graphviz

digraph architecture {
node [color=green] [shape=box]; "A"
node [color=blue] [shape=oval]; "B", "C"
"A" -> "B";
"B" -> "C";
"C" -> "A";
"C" -> "Z";
"Z" -> "A";
}

/posts/testdot.png

You can use a bunch of different shapes.

References

  • https://graphs.grevian.org/
  • https://www.graphviz.org/doc/info/attrs.html

date

I always forget the arguments to date.

Seconds since UNIX epoch

date +%s

Today's date yyyy-mm-dd

$ date -I
2019-07-23
$ date +'%Y-%m-%d'
2019-07-23

Next week's date

Use the -d flag

$ date -d '+1week'
Tue 30 Jul 14:50:03 AEST 2019

Convert local time to UTC

$ TZ=UTC date -d 'TZ="Australia/Sydney" 24 Jun 19 11:45 PM'
Mon 24 Jun 13:45:00 UTC 2019

Difference between two dates

$ START=$(date -d 'TZ="Australia/Sydney" 24 Jun 19 23:45' +'%s')
$ END=$(date -d 'TZ="Australia/Sydney" 25 Jun 19 02:55' +'%s')

# Number of hours between $END and $START
$ echo "scale=2;($END-$START)/3600" | bc
3.16
Next Page