Skip to content

Instantly share code, notes, and snippets.

View wader's full-sized avatar
🦫

Mattias Wadman wader

🦫
View GitHub Profile
@wader
wader / strconvx.go
Created July 28, 2023 14:12
Optionally strict go ParseInt
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// This is a fork of std/strconv ParseInt/ParseUint with a strict
// option to allow trailing invalid characters.
package strconvx
import (
@wader
wader / mpeg_l3.ksy
Created June 21, 2023 17:34
mpeg layer 3 header plus som test
meta:
id: mpeg_l3
seq:
- id: sync
type: b11
- id: mpeg_version
type: b2
enum: mpeg_version
- id: layer
type: b2
@wader
wader / duration.jq
Last active May 5, 2023 14:45
jq duration helpers
# "01:02:03.45" -> 3723.45
def from_duration:
( reduce (split(":") | reverse[]) as $p (
{m: 1, n: 0};
( .n = .n + ($p | tonumber) * .m
| .m *= 60
)
)
| .n
);
@wader
wader / syslog.jq
Created April 3, 2023 10:08
jq syslog parser
def from_syslog:
( split("\n")
| map(
( capture("^(?<ts>\\w{3}\\s+\\d{1,2} +[\\d:]+) (?<host>\\w+) (?<process>\\w+)\\[(?<pid>\\d+)\\]: (?<message>.*)$")
| .ts |= strptime("%b %d %H:%M:%S")
| .pid |= tonumber
)
)
);
@wader
wader / test.md
Last active March 5, 2023 09:25
url dot suffix test
def _cvs_named_color_ranges:
{ all: [[0,255]]
, ascii: [[0,127]]
, nonascii: [[128,255]]
, null: [[0,0]]
, space: [[9,13]]
, print: [[32,126]]
};
# same as fromstream but with a prefix level, outputs [$prefix_path, $json_value], ...
# Usage:
# cat big | jq -L . --stream -n 'include "stream"; reduce fromstream(inputs; 1) as $pv ({}; $pv as [$p, $v] | .[$v.type] += 1)'
def streaks_by(f; g):
foreach ((g | ["v", .]), ["end"]) as $o (
{ acc: null
, fv: null
, emit: null
};
Was created from https://github.com/wader/vscode-jq/blob/master/syntaxes/jq.tmLanguage.json
@wader
wader / gist:ca20a94af162ccbd32dba4724befbca5
Created October 21, 2022 14:52
Convert .tmLangauge.json file to .sublime-syntax
plutil -convert xml1 file.tmLanguage.json -o file.tmLanguage
Open file.tmLanguage in Sublime and use Tools / Developer / "New Syntax from file.tmLanguage"
A new tab with file.subline-syntax should appear
@wader
wader / gist:e601fb51d18a90a66733027c4253cad2
Created December 9, 2021 10:37
Parse broken JSON with jq
echo '[{"a":123, "b":123, "c": 2},' | jq --stream | jq -s 'reduce (.[] | select(.[1])) as $p ({}; setpath($p[0]; $p[1])?)'