Home > Net >  How to get values with yml file with bash?
How to get values with yml file with bash?

Time:08-26

I have this yml file, How do i retrieve only the values under backends with bash? I know there's sed, grep and awk. I do not know what the best way to retrieve the data

affinityCookieTtlSec: 0
backends:
- balancingMode: UTILIZATION
  capacityScaler: 0.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
- balancingMode: UTILIZATION
  capacityScaler: 1.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
connectionDraining:
  drainingTimeoutSec: 300
creationTimestamp: '2021-09-08'
description: ''
enableCDN: false
healthChecks:
- https://www.googleapis.com/compute/
kind: compute#backendService
loadBalancingScheme: EXTERNAL
logConfig:
  enable: false
name: backend
port: 80
portName: portname
protocol: HTTP
selfLink: https://www.googleapis.com/compute/
sessionAffinity: NONE
timeoutSec: 30

Expected output

- balancingMode: UTILIZATION
  capacityScaler: 0.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
- balancingMode: UTILIZATION
  capacityScaler: 1.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8

CodePudding user response:

You can do it simply with awk by setting a flag f true when you want to output the line and then test when beginning characters are no longer ' ' or '-' and quit printing at that point, e.g.

awk '/^backends:/{f=1; next} f {if ($0 ~/^[ -]/) print; else f=0}' file.yml

Instead of setting f=0 you can simply call exit at that point -- but be aware, if you have an END rule, it will be run after exit is called. (there is no END rule here)

A shorter rearrangement could be:

awk '/^backends:/{f=1; next} /^[^ -]/{f=0} f' file.yml

(a bit less readable -- up to you)

Note above, a condition alone, e.g. f (shorthand for f != 0), will invoke the default action of print. So if f != 0, simply providing f as a standalone condition will cause the records (lines) to print while that condition is true without also including the { print } after it.

Example Output

- balancingMode: UTILIZATION
  capacityScaler: 0.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
- balancingMode: UTILIZATION
  capacityScaler: 1.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8

CodePudding user response:

Why not use yq? It's a lightweight and portable command-line YAML, JSON and XML processor, see https://github.com/mikefarah/yq

echo "affinityCookieTtlSec: 0
backends:
- balancingMode: UTILIZATION
  capacityScaler: 0.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
- balancingMode: UTILIZATION
  capacityScaler: 1.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
connectionDraining:
  drainingTimeoutSec: 300
creationTimestamp: '2021-09-08'
description: ''
enableCDN: false
healthChecks:
- https://www.googleapis.com/compute/
kind: compute#backendService
loadBalancingScheme: EXTERNAL
logConfig:
  enable: false
name: backend
port: 80
portName: portname
protocol: HTTP
selfLink: https://www.googleapis.com/compute/
sessionAffinity: NONE
timeoutSec: 30" | yq .backends

Which prints:

- balancingMode: UTILIZATION
  capacityScaler: 0.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
- balancingMode: UTILIZATION
  capacityScaler: 1.0
  group: https://www.googleapis.com/compute/
  maxUtilization: 0.8
  • Related