1
0

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.

ubuntu20.04でyqコマンドを使う

Last updated at Posted at 2022-02-06

yqコマンドについて

yq は YAML を操作できるコマンドラインツール

出典:https://github.com/mikefarah/yq

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/issues/607

参考

https://github.com/mikefarah/yq
Install yq on Ubuntu 20.04
https://mikefarah.gitbook.io/yq/

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?