/gedi

Primary LanguageGoMIT LicenseMIT

gedi

A simple streaming editor like sed and the like, but written in Go and expr.

usage

Each record (depending on the file type, it can be a line(string) / row([]string) / json element(map[string]any)) is read into var x and can be referenced in the expression. By default it operates in filter mode, which simply prints the record if the expression evaluates to true.

gedi -f examples/lines.txt 'atoi(x) % 2 == 0'

Additional vars / functions:

  • ix: the current record number.

  • x: the current record. When filetype is line, it's the current line as string, when filetype is csv, it's the current row as []string.

  • kx: if x comes from an array of a json field, kx is the name of the field. e.g. for {"k": [1,2,3]}, kx would be k.

  • now: time.Now() at the start of the program, provided to avoid calling time.Now() repeatedly in large files.

  • ms: 1 time.Millsecond

  • sec: 1 time.Second

  • min: 1 time.Minute

  • hour: 1 time.Hour

  • day: 24 time.Hour

  • week: 7 * 24 time.Hour

  • month: 30 * 24 time.Hour

  • year: 365 * 24 time.Hour

  • localtime(string): guess time from a given string as if it's a local time. e.g. assuming local is HTK, "2023-01-01 00:00:00 WARN foobar" | localtime() gives 2023-01-01 00:00:00 UTC+08:00

  • utctime(string): guess time from a given string as if it's a UTC time. e.g. "2023-01-01 00:00:00 WARN foobar" | utctime() gives 2023-01-01 00:00:00 UTC+0000

  • tztime(string, string): guess time from a given string as if it's a given timezone time. e.g. "2023-01-01 00:00:00 WARN foobar" | tztime("UTC+8") gives 2023-01-01 00:00:00 UTC+08:00

  • unixtime(int64): converts a given unix timestamp in milliseconds, seconds, microseconds to time.Time.

  • within(time.Time|string, time.Time|string): checks if a given time is within a given duration comapred to now. e.g. find log lines that are within the last 24 hours: x[0:20] | localtime() | within("-24h")

  • after(time.Time|string, time.Time|string): checks if the 1st time on or after the 2nd time.

  • before(time.Time|string, time.Time|string): checks if the 1st time on or before the 2nd time.

  • gt, lt, ge, le: check if the 1st arg(ints, floats, time.Time, time.Duration) is greater than, less than, greater than or equal to, or less than or equal to the 2nd arg (of the same type).

  • grep(input, regex [, group]): grep matches from input. If the regex has groups, the group is the index of the group to return.

TODOs

[x] supports line by line files

[x] supports csv

[x] supports jsonl

[x] supports jsonarray