【问题标题】:Curl Get Specific value from the outputCurl 从输出中获取特定值
【发布时间】:2015-09-16 11:20:48
【问题描述】:

如果我运行它,我有一个 curl 命令,输出如下,

{
  "page" : 1,
  "records" : 1,
  "total" : 1,
  "rows" : [ {
    "automated" : true,
    "collectionProtocol" : "MagBead Standard Seq v2",
    "comments" : "",
    "copy" : false,
    "createdBy" : "stest",
    "custom1" : "User Defined Field 1=",
    "custom2" : "User Defined Field 2=",
    "custom3" : "User Defined Field 3=",
    "custom4" : "User Defined Field 4=",
    "custom5" : "User Defined Field 5=",
    "custom6" : "User Defined Field 6=",
    "description" : null,
    "editable" : false,
    "expanded" : false,
    "groupName" : "99111",
    "groupNames" : [ "all" ],
    "inputCount" : 1,
    "instrumentId" : 1,
    "instrumentName" : "42223",
    "jobId" : 11111,
    "jobStatus" : "In Progress",
    "leaf" : true,
    "modifiedBy" : null,
    "name" : "Copy_of_Test_Running2"
  } ]
}

我只想提取 jobId 的值。 这个输出将是

  11111

如果有多行则有多个jobId

  11111
  11112
  11113

我只想在 while 循环中提取 jobId 和 process。 如下图,

 while read job; do
 echo $job
 done < < (curl command)

我想在另一个命令中使用该作业 ID。 卷曲结果可能是多个。

您是否有想法轻松提取 curl 输出并进行 while 或 for 循环?

【问题讨论】:

标签: json linux loops curl awk


【解决方案1】:

我认为jq(感谢@Mircea)是一个不错的解决方案。

此外,我可以提供一个简单的awk 解决方案,前提是 curl 的输出格式是规范的并且没有任何脏符号。

所以,请小心使用:

while IFS= read -r line
do
    echo $line|awk -F':' '/jobId/{split($2,a,",");for(i in a){if(a[i]){printf("%d\n",a[i])}}}'
done < "$file"

【讨论】:

  • 谢谢,我觉得 jq 很棒,我不需要 awk
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2017-01-14
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多