Saturday, March 12, 2022

[SOLVED] grep specific values from .yml file

Issue

I want to extract a specific value from value.yml file


- slug: 2352T
  en:
    term: "abc"
    def: >
      ASY
      YFH
      UUU
  pt:
    term: "cbb"
    def: >
      ATF
      TTT

- slug: HSOD5
  en:
    term: "cbs"
    def: >
     AEG
     WWW
  af:
    term: "xbb"
    def: >
     AUT

From this file, I want to use grep or anything else to extract only the following values:

- slug: 2352T
  en:
    term: "abc"
    def: >
      ASY
      YFH
      UUU
- slug: HSOD5
  en:
    term: "cbs"
    def: >
      AEG
      WWW

Can you help me with that, please?

I tried this

grep  "slug"  value.yml | awk -F ": " '{print $2}'

and I tried

yq r --printMode pv "slug.en.*"

and tried

yq r value.yml slug.en

and

yq r value.yml --collect slug.en.def

but none of them worked


Solution

As KamilCuk suggested in the comments, use tools designed to parse yaml instead of grep, but if you have to use grep, use something like grep -A4 '^- slug:' in.yml. This approach is not robust and relies on the same order of the yaml fields as you have shown in your example.

You can also use grep -A4 --no-group-separator if you want to remove the -- delimiter that grep -A outputs in between matches.

Example:

Create the example input:

cat > in.yml <<EOF
- slug: 2352T
  en:
    term: "abc"
    def: >
      ASY
  pt:
    term: "cbb"
    def: >
      ATF.

- slug: HSOD5
  en:
    term: "cbs"
    def: >
     AEG
  af:
    term: "xbb"
    def: >
     AUT
EOF

Print the matching lines and the next 4 lines after each match:

grep -A4 '^- slug:' in.yml

Output:

- slug: 2352T
  en:
    term: "abc"
    def: >
      ASY
--
- slug: HSOD5
  en:
    term: "cbs"
    def: >
     AEG

Same, without the dashes between the matching blocks:

grep -A4 --no-group-separator '^- slug:' in.yml

Output:

- slug: 2352T
  en:
    term: "abc"
    def: >
      ASY
- slug: HSOD5
  en:
    term: "cbs"
    def: >
     AEG

SEE ALSO:

grep manual

-A num
--after-context=num
Print num lines of trailing context after matching lines.

--no-group-separator
When -A, -B or -C are in use, do not print a separator between groups of lines.



Answered By - Timur Shtatland
Answer Checked By - David Goodson (WPSolving Volunteer)