1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TOML をコマンドラインで加工したい

Last updated at Posted at 2021-12-22

TOML

TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

TOMLは、セマンティクスが明確で読みやすい、最小限の設定ファイルフォーマットを目指しています。TOMLは、ハッシュテーブルに明確にマッピングするように設計されています。TOMLは、さまざまな言語でデータ構造を簡単に解析できるようになっています。

Rust の設定ファイルとか SSG のツールの設定ファイルとかでよく出くわす気がする。個人的には YAML より見やすくて好き。

jq

jq is a lightweight and flexible command-line JSON processor.

jq は軽量で柔軟なコマンドライン JSON プロセッサです。

便利でメジャー。

dasel

Dasel (short for data-selector) allows you to query and modify data structures using selector strings.
Comparable to jq / yq, but supports JSON, YAML, TOML, XML and CSV with zero runtime dependencies.

Dasel(data-selectorの略)は、セレクタ文字列を使ってデータ構造を照会・修正することができます。
jq / yqに相当しますが、JSON、YAML、TOML、XML、CSVをサポートし、ランタイムへの依存はゼロです。

TOML もサポートしているところがとても便利なんだけど、jq / yq と記法はかなり違う。なので、 JSON/YAML ←→ TOML の相互変換にだけ使って、中身のフィルタリングや加工は jq などでやったほうが楽に感じる(でもこれは慣れの問題で、ちゃんと覚えれば dasel 一本で済ませたほうがシンプルだし快適なのかもしれない)。

jq と dasel の記法の違いはこのドキュメントが分かりやすい。

sample.toml

description = "あいうえお"
keywords = ["test", "example"]
title = "ほげほげ"

[en]
title = "hogehoge"
description = "abcde"

dasel で一度 JSON に変換して、 jq でいじくって、 また dasel で TOML に逆変換。

$ cat sample.toml | dasel -r toml -w json | jq '.keywords = ["テスト", "サンプル"]' | dasel -r json -w toml -m --color
description = "あいうえお"
keywords = ["テスト", "サンプル"]
title = "ほげほげ"

[en]
  description = "abcde"
  title = "hogehoge"

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?