json - 无法在 shell 脚本中使用 sed 提取 json 文件的特定值
问题描述
我正在使用 sed 命令提取执行 curl 后从输出中获得的值。下面是我的shell脚本。
#!/bin/bash
release=$(curl -H "Authorization:token xxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" https://api.github.com/repos/octocat/Hello-World/releases/tags/v1.0.0)
id=$(echo "$release" |sed -n -e 's/"id":\\ \\([0-9]\\+\\),/\\1/p' | head -n 1 | sed 's/[[:blank:]]//g')
echo $id
我在 curl 之后得到的输出是一个 json 文件。下面是输出
{
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
"assets_url": "https://api.github.com/repos/octocat/Hello-
World/releases/1/assets",
"upload_url": "https://uploads.github.com/repos/octocat/Hello-
World/releases/1/assets{?name,label}",
"tarball_url": "https://api.github.com/repos/octocat/Hello-
World/tarball/v1.0.0",
"zipball_url": "https://api.github.com/repos/octocat/Hello-
World/zipball/v1.0.0",
"id": 1234332,
"node_id": "MDc6UmVsZWFzZTE=",
"tag_name": "v1.0.0",
"target_commitish": "master",
"name": "v1.0.0",
"body": "Description of the release",
"draft": false,
"prerelease": false,
"created_at": "2013-02-27T19:35:32Z",
"published_at": "2013-02-27T19:35:32Z",
"author": {
"login": "octocat",
"id": 1353322,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url":
"https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}
{/repo}",
"subscriptions_url":
"https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url":
"https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"assets": [
{
"url": "https://api.github.com/repos/octocat/Hello-
World/releases/assets/1",
"browser_download_url": "https://github.com/octocat/Hello-
World/releases/download/v1.0.0/example.zip",
"id": 56432211,
"node_id": "MDEyOlJlbGVhc2VBc3NldDE=",
"name": "example.zip",
"label": "short description",
"state": "uploaded",
"content_type": "application/zip",
"size": 1024,
"download_count": 42,
"created_at": "2013-02-27T19:35:32Z",
"updated_at": "2013-02-27T19:35:32Z",
"uploader": {
"login": "octocat",
"id": 5663322,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url":
"https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}
{/repo}",
"subscriptions_url":
"https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url":
"https://api.github.com/users/octocat/events{/privacy}",
"received_events_url":
"https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
}
}
]
}
我在上面发布的 sed 命令必须像其他 curl 命令一样工作。
该id
值多次出现,但我希望将第一个值ID
存储在id
变量中"id": 1234332
但作为输出,我什么也没得到。
在这方面需要帮助。
解决方案
- 不要使用双引号而不是单引号,除非您想要扩展引用字符串中的任何内容,
- 使用jq解析 JSON 值,sed 是一个面向行的工具,无法解析 JSON 语法。
例如:
id=$(curl -H 'Authorization:token xxxxxxxxxxxxxxxxxxxxxx' -H 'Content-Type: application/json' 'https://api.github.com/repos/octocat/Hello-World/releases/tags/v1.0.0' |
jq -r '.id')
echo "$id"
推荐阅读
- python-3.x - 映射函数python3以获取消息长度
- ios - Swift 警告:应用程序委托收到对 -application:performFetchWithCompletionHandler 的调用:但从未调用过完成处理程序
- swift - 删除具有子值的父节点
- c++ - 在 C++ 中使用 libevdev 的设备输入事件
- java - ClassCastException:java.math.BigInteger 不能在 DriverManager.getConnection() 上转换为 java.lang.Long
- javascript - 尝试将静态 javascript 导入百里香
- javascript - vue.js :在需要该值的第二个 api 调用运行之前,变异状态变量尚未完成
- api - 如何提取 Google 服务帐户的令牌?
- reflection - Julia 反思是否提供内省和代祷?
- python - 在单个会话中从 python 运行 bash 命令