使用yq读取yml文件的值

        yq一个轻量级和便携式命令行YAML处理器,yq是用go编写的,所以它是一个无依赖性的二进制文件,可以直接运行在CentOS等其他发行版的Linux上

        如果需要使用shell脚本去读取或修改某个yaml的键值,那么使用yq很方便的达到目的,下面将演示如果在命令行下使用yq进行读取或修改yaml文件的键值

下载地址

        GitHub - mikefarah/yq: yq is a portable command-line YAML, JSON and XML processor

创建测试yaml文件

[root@test ~]# cat > /root/test.yml < a:
>   b:
>     c: 123
> d:
>   e: 456
> f: 789
> EOF

使用演示

1.读取yaml文件中的一个值

[root@test ~]# /root/yq_linux_amd64 '.a.b.c' /root/test.yml 
123
[root@test ~]# /root/yq_linux_amd64 '.d.e' /root/test.yml 
456
[root@test ~]# /root/yq_linux_amd64 '.f' /root/test.yml 
789

2.从STDIN中读取一个值

[root@test ~]# cat /root/test.yml | /root/yq_linux_amd64 '.a.b.c'
123
[root@test ~]# cat /root/test.yml | /root/yq_linux_amd64 '.d.e'
456
[root@test ~]# cat /root/test.yml | /root/yq_linux_amd64 '.f'
789

3.为目标文件更新一个值

[root@test ~]# cat /root/test.yml      #修改前的yaml文件
a:
  b:
    c: 123
d:
  e: 456
f: 789
[root@test ~]# /root/yq_linux_amd64 -i '.a.b.c=11111' /root/test.yml    #更新a.b.c的值为11111
[root@test ~]# echo $?
0
[root@test ~]# cat /root/test.yml       #修改后的yaml文件
a:
  b:
    c: 11111
d:
  e: 456
f: 789

#修改原键值成功

4.为目标文件追加一个新值

[root@test ~]# cat /root/test.yml     #修改前的yaml文件
a:
  b:
    c: 11111
d:
  e: 456
f: 789
[root@test ~]# /root/yq_linux_amd64 -i '.gggg=22222' /root/test.yml    #追加一个gggg=2222
[root@test ~]# echo $?
0
[root@test ~]# cat /root/test.yml     #追加后的yaml文件
a:
  b:
    c: 11111
d:
  e: 456
f: 789
gggg: 22222

#追加新键值成功

5.使用环境变量更新/或追加值

[root@test ~]# cat /root/test.yml   #修改前的yaml文件
a:
  b:
    c: 11111
d:
  e: 456
f: 789
gggg: 22222
[root@test ~]# test_key=33333  /root/yq_linux_amd64 -i '.hhhh = strenv(test_key)' /root/test.yml                 #追加一个hhhh键,值为$test_key
[root@test ~]# echo $?
0
[root@test ~]# cat /root/test.yml  #修改后的文件
a: 
  b:
    c: 11111
d:
  e: 456
f: 789
gggg: 22222
hhhh: "33333"

#使用环境变量修改/追加键值成功

使用帮助

Usage:
  yq [flags]
  yq [command]

Examples:

# yq defaults to 'eval' command if no command is specified. See "yq eval --help" for more examples.

# read the "stuff" node from "myfile.yml"
cat myfile.yml | yq '.stuff' 

# update myfile.yml in place
yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace


Available Commands:
  completion       Generate the autocompletion script for the specified shell
  eval             (default) Apply the expression to each document in each yaml file in sequence
  eval-all         Loads _all_ yaml documents of _all_ yaml files and runs expression once
  help             Help about any command
  shell-completion Generate completion script

Flags:
  -C, --colors                        force print with colors
  -e, --exit-status                   set exit status if there are no matches or null or false is returned
      --expression string             forcibly set the expression argument. Useful when yq argument detection thinks your expression is a file.
  -f, --front-matter string           (extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact
      --header-preprocess             Slurp any header comments and separators before processing expression. (default true)
  -h, --help                          help for yq
  -I, --indent int                    sets indent level for output (default 2)
  -i, --inplace                       update the file inplace of first file given.
  -p, --input-format string           [yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml. (default "yaml")
  -M, --no-colors                     force print with no colors
  -N, --no-doc                        Don't print document separators (---)
  -n, --null-input                    Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.
  -o, --output-format string          [yaml|y|json|j|props|p|xml|x] output format type. (default "yaml")
  -P, --prettyPrint                   pretty print, shorthand for '... style = ""'
  -s, --split-exp string              print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter.
      --unwrapScalar                  unwrap scalar, print the value with no quotes, colors or comments (default true)
  -v, --verbose                       verbose mode
  -V, --version                       Print version information and quit
      --xml-attribute-prefix string   prefix for xml attributes (default "+")
      --xml-content-name string       name for xml content (if no attribute name is present). (default "+content")

Use "yq [command] --help" for more information about a command.

你可能感兴趣的:(linux,xml)