我如何使用任何命令来剪切命令输出
Posted
技术标签:
【中文标题】我如何使用任何命令来剪切命令输出【英文标题】:how can i use any command for cut an command output 【发布时间】:2020-10-09 09:29:13 【问题描述】:我要剪切的命令输出:
"logging_page_id":"profilePage_28345663","show_suggested_profiles":false,"show_follow_dialog":false,"graphql":"user":"biography":"","blocked_by_viewer":false,"business_email":null,"restricted_by_viewer":null,"country_block":false,"external_url":null,"external_url_linkshimmed":null,"edge_followed_by":"count":11,"followed_by_viewer":false,"edge_follow":"count":1,"follows_viewer":false,"full_name":"Ellen","has_ar_effects":false,"has_clips":false,"has_guides":false,"has_channel":false,"has_blocked_viewer":false,"highlight_reel_count":0,"has_requested_viewer":false,"id":"28345663","is_business_account":false,"is_joined_recently":false,"business_category_name":null,"overall_category_name":null,"category_enum":null,"is_private":false,"is_verified":false,"edge_mutual_followed_by":"count":0,"edges":[],"profile_pic_url":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","profile_pic_url_hd":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","requested_by_viewer":false,"username":"ebh14","connected_fb_page":null,"edge_felix_video_timeline":"count":0,"page_info":"has_next_page":false,"end_cursor":null,"edges":[],"edge_owner_to_timeline_media":"count":9,"page_info":"has_next_page":false,"end_cursor":null,"edges":["node":"__typename":"GraphImage","id":"202998178801934666","shortcode":"LRMcShimFK","dimensions":"height":612,"width":6
如何在 bash 中获得这样的输出:
full name : Ellen
edge_followed_by : 1
followed_by_viewer : 11
is_business_account : false
business_email : null
is_verified : false
我尝试了类似的命令;
jq
cut
但我做不到我想要的
【问题讨论】:
该行看起来已损坏。dimensions":"height":612,"width":6
不是有效的 json 对象。
您首先提到的是输出吗?如果整个输出与 json 格式匹配,请使用 jq
@LuanPhan 我注意到 OP 已 尝试过jq
- 但我猜它失败了,因为它无效json。
我看到键 "edge_followed_by" 的值为 "count":11 ,键 "followed_by_viewer" 的值为 false。看起来很奇怪,上面的预期。我需要更多说明来定义规则:)
我使用的命令:curl -s instagram.com/ebh14\?__a\=1
【参考方案1】:
由于末尾缺少一些文本,您发布的 JSON 似乎无效(一些关闭 ] 和 似乎丢失)
正确的 JSON 是:
"logging_page_id":"profilePage_28345663","show_suggested_profiles":false,"show_follow_dialog":false,"graphql":"user":"biography":"","blocked_by_viewer":false,"business_email":null,"restricted_by_viewer":null,"country_block":false,"external_url":null,"external_url_linkshimmed":null,"edge_followed_by":"count":11,"followed_by_viewer":false,"edge_follow":"count":1,"follows_viewer":false,"full_name":"Ellen","has_ar_effects":false,"has_clips":false,"has_guides":false,"has_channel":false,"has_blocked_viewer":false,"highlight_reel_count":0,"has_requested_viewer":false,"id":"28345663","is_business_account":false,"is_joined_recently":false,"business_category_name":null,"overall_category_name":null,"category_enum":null,"is_private":false,"is_verified":false,"edge_mutual_followed_by":"count":0,"edges":[],"profile_pic_url":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","profile_pic_url_hd":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","requested_by_viewer":false,"username":"ebh14","connected_fb_page":null,"edge_felix_video_timeline":"count":0,"page_info":"has_next_page":false,"end_cursor":null,"edges":[],"edge_owner_to_timeline_media":"count":9,"page_info":"has_next_page":false,"end_cursor":null,"edges":["node":"__typename":"GraphImage","id":"202998178801934666","shortcode":"LRMcShimFK","dimensions":"height":612,"width":6]
修复 JSON 后,jq 似乎工作正常
echo '"logging_page_id":"profilePage_28345663","show_suggested_profiles":false,"show_follow_dialog":false,"graphql":"user":"biography":"","blocked_by_viewer":false,"business_email":null,"restricted_by_viewer":null,"country_block":false,"external_url":null,"external_url_linkshimmed":null,"edge_followed_by":"count":11,"followed_by_viewer":false,"edge_follow":"count":1,"follows_viewer":false,"full_name":"Ellen","has_ar_effects":false,"has_clips":false,"has_guides":false,"has_channel":false,"has_blocked_viewer":false,"highlight_reel_count":0,"has_requested_viewer":false,"id":"28345663","is_business_account":false,"is_joined_recently":false,"business_category_name":null,"overall_category_name":null,"category_enum":null,"is_private":false,"is_verified":false,"edge_mutual_followed_by":"count":0,"edges":[],"profile_pic_url":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","profile_pic_url_hd":"https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39","requested_by_viewer":false,"username":"ebh14","connected_fb_page":null,"edge_felix_video_timeline":"count":0,"page_info":"has_next_page":false,"end_cursor":null,"edges":[],"edge_owner_to_timeline_media":"count":9,"page_info":"has_next_page":false,"end_cursor":null,"edges":["node":"__typename":"GraphImage","id":"202998178801934666","shortcode":"LRMcShimFK","dimensions":"height":612,"width":6]' | jq
"logging_page_id": "profilePage_28345663",
"show_suggested_profiles": false,
"show_follow_dialog": false,
"graphql":
"user":
"biography": "",
"blocked_by_viewer": false,
"business_email": null,
"restricted_by_viewer": null,
"country_block": false,
"external_url": null,
"external_url_linkshimmed": null,
"edge_followed_by":
"count": 11
,
"followed_by_viewer": false,
"edge_follow":
"count": 1
,
"follows_viewer": false,
"full_name": "Ellen",
"has_ar_effects": false,
"has_clips": false,
"has_guides": false,
"has_channel": false,
"has_blocked_viewer": false,
"highlight_reel_count": 0,
"has_requested_viewer": false,
"id": "28345663",
"is_business_account": false,
"is_joined_recently": false,
"business_category_name": null,
"overall_category_name": null,
"category_enum": null,
"is_private": false,
"is_verified": false,
"edge_mutual_followed_by":
"count": 0,
"edges": []
,
"profile_pic_url": "https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39",
"profile_pic_url_hd": "https://instagram.fadd1-1.fna.fbcdn.net/v/t51.2885-19/11906086_160969117570026_1944988813_a.jpg?_nc_ht=instagram.fadd1-1.fna.fbcdn.net&_nc_ohc=6zVEF7PrIwcAX_x7UQ6&oh=79f8004d8b72cc242a02a703a9304a27&oe=5FAB9F39",
"requested_by_viewer": false,
"username": "ebh14",
"connected_fb_page": null,
"edge_felix_video_timeline":
"count": 0,
"page_info":
"has_next_page": false,
"end_cursor": null
,
"edges": []
,
"edge_owner_to_timeline_media":
"count": 9,
"page_info":
"has_next_page": false,
"end_cursor": null
,
"edges": [
"node":
"__typename": "GraphImage",
"id": "202998178801934666",
"shortcode": "LRMcShimFK",
"dimensions":
"height": 612,
"width": 6
]
【讨论】:
【参考方案2】:在文本中使用以下内容应该可以满足您的需要:
jq '.graphql.user.full_name,.graphql.user.edge_followed_by.count,.graphql.user.followed_by_viewer,.graphql.user.is_business_account,.graphql.user.business_email,.graphql.user.is_verified'
【讨论】:
以上是关于我如何使用任何命令来剪切命令输出的主要内容,如果未能解决你的问题,请参考以下文章