我試圖在shell中從curl命令的輸出中提取一個url,
。我正在運行的curl命令是:
curl -s "http://HOSTNAME.com/api/v4/projects/18/merge_requests?source_branch=samm_6819"
這樣的輸出是這樣的 :
[{"id": 244,"iid"/span>:69,"project_id"/span>:18,"title"/span>: "bug 6819","description":"","state"。 "合并","created_at":"2021-09-04T06:51:05. 988Z","updated_at":"2021-09-04T06:52:03. 869Z"/span>,"merged_by"/span>:{"id"/span>: 4,"name":"SOME NAME","username"。 "SOMEUSERNAME","state":"active","avatar_url":"https://www. gravatar. com/avatar/baa4538f891a621a8e5480aa9ac404a6? s=80u0026d=identicon","web_url":"http://HOSTNAME/SOMEUSERNAME"},"merged_at":"2021-09-04T06:52:03. 997Z","closed_by":null,"closed_at": null,"target_branch":"master"," source_branch": "samm_6819","user_notes_count":0,"upvotes":0,"downnvotes": 0,"author":{"id":1,"name":" Administrator","username": "root","state":"active", "avatar_url":"https://www. gravatar.com/avatar/81fasf149c17eba1d66803dc0877828900? s=80u0026d=identicon","web_url":"http://HOSTNAME/root"},"assignees"。 [],"assignee":null,"reviewers":[],"source_project_id":18,"target_project_id": 18,"labsels":[],"draft":false,"work_in_progress": false,"milestone":null,"merge_when_pipeline_succeeds": true,"merge_status":"can_be_merged", "sha": "1e14427dd70862265b55fsa0e38f4e980d5f65524","merge_commit_sha": "34240a857d7d1a852f9c3d4safa3f031ef3bd35225","squash_commit_sha": null,"discussion_locked": null,"should_remove_source_branch": false,"force_remove_source_branch":false," reference":" ! 69","references":{"short":" ! 69","相對":"! 69","完整":"SOMEPROJECTNAME-AF/test! "},"web_url"/span>:"http://HOSTNAME. com/GROUPNAME/PROJECTNAME/-/merge_requests/69","time_stats":{"time_estimate":0,"total_time_spent": 0,"human_time_estimate":null,"human_total_time_spent":null},"scash": false,"task_completion_status"/span>:{"count"/span>: 0,"completed_count":0}, "has_conflicts": false,"blocking_discussions_resolved":true,"approvals_before_merge":null}
在所有這些資料中,我需要輸出的是:
http://HOSTNAME.com/GROUPNAME/PROJECTNAME/-/merge_requests/69
我需要這個網址,我需要在某個地方列印。我不確定在這里可以使用awk、sed、cut或任何帶有curl命令的管道來獲得這個url的輸出。
有人能幫助我嗎? 我從來沒有想過要在這里問這個問題,但我已經沒有選擇了,什么是實作這一目標的最佳方式。
謝謝你
uj5u.com熱心網友回復:
這里最好的選擇是使用jq:
json='[{"id":244,"iid":69,"project_id":18,"title":"bug 6819","description":"","state":"merged","created_at":"2021-09-04T06:51:05. 988Z","updated_at":"2021-09-04T06:52:03.869Z","merged_by":{"id":4,"name":"SOME NAME ","username":"SOMEUSERNAME","state":"active","avatar_url":"https://www.gravatar. com/avatar/baa4538f891a621a8e5480aa9ac404a6?s=80u0026d=identicon","web_url":"http://HOSTNAME/SOMEUSERNAME"},"merged_at":"2021-09-04T06:52:03. 997Z", "closed_by":null, "closed_at":null, "target_branch": "master", "source_branch": "samm_6819", "user_notes_count":0, "upvotes":0, "downvotes":0, "author":{"id":1, "name": "administration", "username": "root", "state": "active", "avatar_url": "https://www. gravatar.com/avatar/81fasf149c17eba1d66803dc0877828900? s=80u0026d=identicon", "web_url": "http://HOSTNAME/root"}, "assignees":[], "assignee":null, "reviewers":[], "source_project_id":18, "target_project_id":18, "labs":[], "draft": false, "work_in_progress": false, "milestone": null, "merge_when_pipeline_succeeds": true, "merge_status": "can_be_merged","sha":"1e14427dd70862265b55fsa0e38f4e980d5f65524","merge_commit_sha":"34240a857d7d1a852f9c3d4safa3f031ef3bd35225","squash_commit_sha": null, "discussion_locked":null, "should_remove_source_branch":false, "force_remove_source_branch": false, "reference":"! 69","references":{"short":"!69","relative":"!69","full":"SOMEPROJECTNAME-af/test!69 "}, "web_url": "http://HOSTNAME. com/GROUPNAME/PROJECTNAME/-/merge_requests/69", "time_stats":{"time_estimate":0, "total_time_spent":0, "human_time_estimate": null, "human_total_time_spent": null}, "squash": false, "task_completion_status":{"count":0, "completed_count":0}, "has_conflicts":false, "blocking_discussions_resolved":true, "approvals_before_merge": null}]'
echo $json | jq -r '. [].web_url'
uj5u.com熱心網友回復:
你可以試試這個 sed
sed 's/.*:. (http. [^"]*).*/1/'
它將匹配http的最后一次出現,直到http之后"的第一次出現。
uj5u.com熱心網友回復:
使用cat file代替curl...進行演示,這將對你顯示的輸入進行操作,在每個Unix盒子的任何shell中使用任何sed:
$ cat file | sed 's/.*"web_url":"([^"]*).*/1/'
http://HOSTNAME.com/GROUPNAME/PROJECTNAME/-/merge_requests/69
uj5u.com熱心網友回復:
嘗試Perl
$ perl -ne ' /.*"web_url":"([^"] )"/ and print "$1
" ' Sameer.txt
http://HOSTNAME.com/GROUPNAME/PROJECTNAME/-/merge_requests/69
另一種方法:
perl -ne ' while( /"web_url":"( [^"] )"/g ) { $x=1; $t=$1 } print "$t
" if $x; $x=0 ' sameer.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319774.html
標籤:
