一致する検索値に基づいて特定の値を見つける方法

一致する検索値に基づいて特定の値を見つける方法

"db-unique-name"見つけるのに助けが必要ですlifecycle-state": "AVAILABLE"

cat db_systems.txt

     "db-unique-name": "p00z5bj_iad2bj",
      "db-workload": "OLTP",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "dfadfasfsadfasdfasdf",
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
--
      "db-unique-name": "p00u5bh_iad2bj",
      "db-workload": "OLTP",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "asdfsadfasdfasfd",
      "lifecycle-details": "Resource was terminated at the backend.",
      "lifecycle-state": "FAILED",
--
      "db-unique-name": "p00u5bh_iad2bj",
      "db-workload": "OLTP",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "asdfasdfasdf",
      "lifecycle-details": "Resource was terminated at the backend.",
      "lifecycle-state": "FAILED",
    enter code here

"db-unique-name"に基づいた試行値"lifecycle-state": "AVAILABLE"。フォローしますが、間違った値を提供しています。

cat db_systems.txt  |  egrep -A -6  "lifecycle-state|AVAILABLE" | grep  db-unique-name
"db-unique-name": "p00u5bh_iad2bj",
"db-unique-name": "p00u5bh_iad2bj",

私もこれを試しましたが、すべてリストされています。

cat db_systems.txt |  awk -F";" '{for(i=1;i<=NF;i++){if ($i ~ /AVAILABLE|db-unique-name/){print $1}}}' | grep db-unique-name
"db-unique-name": "p00z5bj_iad2bj",
"db-unique-name": "p00u5bh_iad2bj",
"db-unique-name": "p00u5bh_iad2bj",

ベストアンサー1

GNUで試してみてくださいawk

awk -F',' 'BEGIN { RS = "--" } /"lifecycle-state": "AVAILABLE"/ {  gsub("^[[:blank:]]*", "", $1);  print $1 }' file

出力:

"db-unique-name":"p00z5bj_iad2bj"

おすすめ記事