-1

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
Mr.Nobody
  • 37
  • 3
  • Use `awk`. When the line matches `/backends:/`, start reading and printing lines until you get another line that begins with a letter. Then exit. – Barmar Aug 26 '22 at 02:25
  • This may help: https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or https://stackoverflow.com/questions/17988756/how-to-select-lines-between-two-marker-patterns-which-may-occur-multiple-times-w using those answers: `awk '/^backends:/{flag=1;next}/^[^- ]/{flag=0}flag' file` – Jerry Jeremiah Aug 26 '22 at 02:31
  • Or maybe `grep`: `grep -A 8 '^backends:' your.yml | grep '^-\|^ '` or maybe `grep -A 8 '^backends:' your.yml | tail -n 8` (Although, the `awk` option suggested by @JerryJeremiah would be my preference) – j_b Aug 26 '22 at 02:55

2 Answers2

1

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
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

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
ryenus
  • 15,711
  • 5
  • 56
  • 63