Skip to content

Instantly share code, notes, and snippets.

@detj
Last active September 7, 2024 02:44
Show Gist options
  • Save detj/0b97504eef18f20ae3425e549689e10a to your computer and use it in GitHub Desktop.
Save detj/0b97504eef18f20ae3425e549689e10a to your computer and use it in GitHub Desktop.
Munging data on terminal
# Manipulating data by combining various modern terminal utilities like fd, dasel, rg and so on.
# Working with single files. Let's assume there is a `data.json` file with the following contents.
# [
# {
# "id": "some-id-1",
# "attribute": {
# "some-key-1": "some-value-1",
# "some-key-2": "some-value-2",
# "some-key-3": "some-value-3"
# }
# },
# {
# "id": "some-id-2",
# "attribute": {
# "some-key-1": "some-value-1",
# "some-key-2": "some-value-2",
# "some-key-3": "some-value-3"
# }
# }
# ]
#
# Know more
# https://github.com/TomWright/dasel
# https://github.com/sharkdp/fd
# https://github.com/BurntSushi/ripgrep
# Output JSON
dasel -r json -f data.json
# Modify JSON
dasel put -r json -f data.json -t string -v whatever 'all().attribute.some-key-2'
# Show list of files with matches
fd -e json | xargs -I {} rg --files-with-matches '"some-key"' {}
# Show each match with 10 lines of before and after context
fd -e json | xargs -I {} rg -A 10 '"some-key"' {}
# Show filename for cluster of matches
fd -e json | xargs -I {} rg -A 10 --with-filename '"some-key"' {}
# Selet all objects matching a nested object's property value
dasel -r json -f data.json '.all().filter(equal(key,value))'
# Modify deeply nested object's property value
dasel put -r json -f data.json -t int -v 999999 '.all().filter(equal(key,value)).foo.bar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment