Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

YAMLをコマンドでパースできるツール「yq」

ちょっと前に、JSONをパースしてくれる便利コマンド「jq」を紹介しました。

CLI上でjsonパースをしてくれるjqを使えばワンライナーが不要となる - Bye Bye Moore

今日散歩中にフと、あの発想でYAML版つくれないかな……と思い検索をかけたところ……
あ、ありました先行プロジェクトが!
ただしPython(発狂

導入法

jqコマンドとpythonの開発環境をそろえた上で、pipから導入します

$ pip install yq
$ cat mydata.yaml
## global definitions
global:
  debug: yes
  verbose: no
  debugging:
    detailed: no
    header: "debugging started"

## output
output:
  file: "yes"


$ cat mydata.yaml | yq '.'
{
  "global": {
    "debug": true,
    "debugging": {
      "header": "debugging started",
      "detailed": false
    },
    "verbose": false
  },
  "output": {
    "file": "yes"
  }
}

絞り込みもjqと同様

$ cat mydata.yaml | yq '.["output"]'
{
  "file": "yes"
}

単純な構造のYAMLファイルが欲しいだけの場合、以下のような方法もあります。
shuzo-kino.hateblo.jp


参考もと