繁体   English   中英

如何通过 Linux 命令“grep”获取值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM