"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"
答え2
jq
JSON形式のデータを処理する場合(スニペットは次のとおり)、このタイプのデータ操作に非常に役立つツールが何であるかを確認する必要があります。
データが次のような場合
{
"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"
}
だからこのjq
声明は
jq 'select(."lifecycle-state" == "AVAILABLE") | ."db-unique-name" ' < db_systems.txt
出力されます
"p00z5bj_iad2bj"
ただし、ファイルが実際に提供された例と似ている場合(--
区切り文字として使用し、{}オブジェクト表記なし)、回避策が簡単になるawk
可能性があります。 JSON以外のデータをjqにプッシュするのは少し難しいです。
答え3
この試み、
使用grep
:
grep -B6 AVAILABLE file | grep db-unique-name
"db-unique-name": "p00z5bj_iad2bj",
B
行を一致させる前にNUM行先行コンテキストを印刷します。
使用awk
:
awk '{a[++i]=$0;}/AVAILABLE/{print a[NR-6];}' file
"db-unique-name": "p00z5bj_iad2bj",
答え4
最初に配列を作成し(f[]
以下を参照)、各要素名をその値にマッピングするこのアプローチを使用すると、名前で各フィールドにアクセスできるため、複雑な条件を作成して任意の順序でフィールドを印刷できます。
$ cat tst.awk
{
gsub(/^[[:space:]]*"|"?,[[:space:]]*$/,"")
tag = val = $0
sub(/".*$/,"",tag)
sub(/.*"/,"",val)
f[tag] = val
}
/^--/ { prt() }
END { prt() }
function prt() {
if ( f["lifecycle-state"] == "AVAILABLE" ) {
print f["db-unique-name"]
}
delete f
}
$ awk -f tst.awk file
p00z5bj_iad2bj
たとえば、
$ cat tst.awk
BEGIN { OFS="," }
{
gsub(/^[[:space:]]*"|"?,[[:space:]]*$/,"")
tag = val = $0
sub(/".*$/,"",tag)
sub(/.*"/,"",val)
f[tag] = val
}
/^--/ { prt() }
END { prt() }
function prt() {
recNr++
if ( (f["lifecycle-state"] == "FAILED") || ( (f["db-unique-name"] ~ /bh/) && (f["db-workload"] == "OLTP") ) ) {
print recNr, f["lifecycle-details"], f["id"], f["db-unique-name"]
}
delete f
}
$ awk -f tst.awk file
2,Resource was terminated at the backend.,asdfsadfasdfasfd,p00u5bh_iad2bj
3,Resource was terminated at the backend.,asdfasdfasdf,p00u5bh_iad2bj