By Anmol Tomar on CodeX
Nov 29, 2022
By Anmol Tomar on CodeX
Nov 29, 2022
Useful notes about using jq.
jq -c paths
Prints each path as an array and doesn't show values.jq --stream 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' example.json. and array indices were enclosed in square
brackets, then each one would be a valid jq query string.| #!/bin/sh -- | |
| # Set timestamps of files in a cloned git repository to when they were last committed. | |
| # Default: All git-controlled files in current directory, recursively | |
| if [ ${#} = 0 ]; then | |
| files="$(git ls-files -z | xargs -0 echo)" | |
| else | |
| files="${@}" | |
| fi |
| import boto3 | |
| import os | |
| from contextlib import closing | |
| from botocore.exceptions import ClientError | |
| def object_exists(s3, bucket, key): | |
| try: | |
| boto3.resource('s3').Object(bucket, key).load() | |
| return True |
| #!/bin/sh -- | |
| # Toggle macOS Background Sounds on or off. | |
| # | |
| # See the System Settings panel for Accessibility > Audio to change settings | |
| # like the sound played, volume level, and automatic stop. | |
| # `read` returns an integer, but… | |
| if [ $(defaults read com.apple.ComfortSounds 'comfortSoundsEnabled') = 0 ] | |
| then |
| #!/bin/sh | |
| awk 'function wl() { | |
| rate=64000; | |
| return (rate/160)*(0.87055^(int(rand()*10)))}; | |
| BEGIN { | |
| srand(); | |
| wla=wl(); | |
| while(1) { | |
| wlb=wla; | |
| wla=wl(); |
| def recursiveFormat(args, **kwargs): | |
| """ | |
| Recursively apply `string.format()` to all strings in a data structure. | |
| This is intended to be used on a data structure that may contain | |
| format strings just before it is passed to `json.dump()` or `dumps()`. | |
| Ideally, I'd like to build this into a subclass of `json.JsonEncoder`, | |
| but it's tricky to separate out string handling in that class. I'll | |
| continue to think about it. |
| #!/bin/sh -- | |
| # Get the local ngrok public URL, which includes the hostname, extracted using jq | |
| # Inspiration: https://gist.github.com/rjz/af40158c529d7c407420fc0de490758b#gistcomment-2594627 | |
| curl --silent http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url' |
| import re | |
| from functools import reduce | |
| import string | |
| def durationToSecondsRegex(duration: str) -> int: | |
| """ | |
| Parse duration string; return integer number of seconds. | |
| For example, the duration string "1h2m3s" (1 hour, 2 minutes, 3 seconds) | |
| would return 3723 seconds. |