yqコマンドについて
yq は YAML を操作できるコマンドラインツール
install
https://github.com/mikefarah/yq/#install
Install yq on Ubuntu 20.04
curl -LJO https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo mv yq_linux_amd64 /usr/local/bin/yq
sudo chmod a+x /usr/local/bin/yq
$ yq --version
yq (https://github.com/mikefarah/yq/) version 4.19.1
使い方
以下のようなyamlファイルがあったとする
file.yaml
a:
b:
- c: cool
x:
y:
z: foobar
person:
name: ""
以下のように値にアクセスする
$ yq . file.yaml
a:
b:
- c: cool
x:
y:
z: foobar
person:
name: ""
$ yq .a.b[0].c file.yaml
cool
$ yq .x.y.z file.yaml
foobar
以下のように値を更新する
$ yq -i '.a.b[0].c = "super_cool"' file.yaml
$ yq . file.yaml
a:
b:
- c: super_cool
x:
y:
z: foobar
person:
name: ""
$ yq .a.b[0].c file.yaml
super_cool
シェルスクリプト化する場合は""ダブルクオーテーション
でくくる
test.sh
STR="so_much_coool"
yq -i ".a.b[0].c = \"${STR}\"" file.yaml
echo "---"
yq '.a.b[0].c' file.yaml
$ bash test.sh
---
so_much_coool
error Error: Parsing expression: Lexer error: could not match text starting at 1:1 failing at 1:3.
使い方が間違っているのでチュートリアルページを見てやりたいことをみなおす
Error: Parsing expression: Lexer error: could not match text starting at 1:1 failing at 1:3.
unmatched text: "wr"
参考
https://github.com/mikefarah/yq
Install yq on Ubuntu 20.04
https://mikefarah.gitbook.io/yq/