如何根据匹配的搜索值来 grep 特定值

如何根据匹配的搜索值来 grep 特定值

我需要帮助才能"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

如果您正在处理 JSON 格式的数据(片段看起来像这样),您当然应该看看jq哪个是处理此类数据的非常有用的工具。

如果你的数据看起来像这样

{
    "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"

但是,如果您的文件实际上看起来像您给出的示例(使用--as 分隔符且没有 {} 对象表示法),那么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

相关内容