![](/img/trans.png)
[英]How to get the no of matched occurrences using grep command in linux?
[英]How to get the value via Linux command 'grep'
我想通過 docker api 獲得一些具體信息。 在這種情況下,我想使用命令docker images
獲取與REPOSITORY
的<none>
對應的IMAGE ID
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ebeb5aebbeef 5 minutes ago 479MB
build-embed latest 80636e4726f8 10 minutes ago 479MB
<none> <none> a2abf2e07bc3 About an hour ago 1.38GB
ubuntu 18.04 4e5021d210f6 5 weeks ago 64.2MB
我嘗試了如下命令:
$ docker images | grep `<none>'
並得到結果:
<none> <none> ebeb5aebbeef 2 minutes ago 479MB
<none> <none> a2abf2e07bc3 58 minutes ago 1.38GB
我怎樣才能只獲得IMAGE ID
? (如ebeb5aebbeef a2abf2e07bc3
)
The purpose of this question is that I want to remove <none>
images with docker api, like docker rmi $(docker images | grep <none>
謝謝你們
使用awk
僅獲取匹配行的第三個字段。
docker images | awk '$1 == "<none>" { print $3 }'
不要使用grep '<none>'
因為它可以在REPOSITORY
以外的字段中匹配。
將--filter選項與-q
選項一起使用,而不是列出所有圖像並使用grep
過濾它們
docker images -f "dangling=true" -q
docker images
命令有幾個選項可以更直接地執行此操作,而無需嘗試解析其 output。
docker images -f
可以過濾在一組最小條件下呈現的圖像列表。 其中一個條件是“懸空”,也就是那些帶有<none>
標簽的圖像。 這可以替換您的grep
命令。
docker images -q
僅打印圖像 ID 而不是該行的 rest。 這可以替換@Barmar's answer中的awk
命令。
這會讓你
docker images -f dangling=true -q
打印出<none>
圖像的圖像 ID,這就是您所追求的; 從而
docker images -f dangling=true -q | xargs docker rmi
刪除它們。
還要考慮docker system prune
,這將刪除懸空圖像,並停止容器和未使用的網絡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.