0

I am trying to compare these two YAML files and trying to extract the key-value pair from the first file if key matches between these two files. Look at the expected output for more clarity.

YAML File 1:

  COMMON-VAR: "[\n  { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n  { \"DESC\": \"B\", \"O\": \"u\", \"C\":0.8 }\n { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n "
  LASTNAME: "D"
  FIRSTNAME: "JOHN"
  UNKNOWN: "!!"

YAML File 2:

  COMMON-VAR: "[\n  { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n  { \"DESC\": \"B\", \"O\": \"u\", \"C\":0.8 }\n"
  LASTNAME: "E"
  FIRSTNAME: "JOHN"
  ADDRESS: "SanJose, CA"
  ZIP: "09817"

Expected output:

  COMMON-VAR: "[\n  { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n  { \"DESC\": \"B\", \"O\": \"u\", \"C\":0.8 }\n { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n "
  LASTNAME: "D"

My Attempt:

join <(sort file1.yaml) <(sort file2.yaml) | awk '$2!=$3{print $1,$3}' | yq e -I4

It works when I don't have curly braces in my values, but awk fails when I have {} in my value in yaml file.

4

1 回答 1

3

One awk idea:

awk -F':' '
FNR==NR                    { seen[$1]=$0; next }
$1 in seen && $0!=seen[$1] { print seen[$1] }
' file1.yaml file2.yaml

Or, slightly less typing if files are fed in reverse order:

awk -F':' '
FNR==NR                    { seen[$1]=$0; next }
$1 in seen && $0!=seen[$1]
' file2.yaml file1.yaml

Both of these generate:

  COMMON-VAR: "[\n  { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n  { \"DESC\": \"B\", \"O\": \"u\", \"C\":0.8 }\n { \"DESC\": \"A\", \"O\": \"h\", \"confidence\":0.8 },\n "
  LASTNAME: "D"
于 2021-08-06T17:04:49.620 回答