Docker: 如何使用 --format 打印字段的最后 n 个字符
Docker: How to print the last n characters of a field using --format
我正在尝试编写一个 docker 格式化程序,使 docker container ls
的输出更具可读性。我想截断图像字段,以便只打印最后 n
个字符(我处理的一些图像有很长的路径)。
例如:如何修改 {{.Image}}
以便输出来自:
docker container ls --format "table {{.ID}}\t{{.Image}}"
43b15196a114 nginx:1.17.2-alpine
5cfb448d2aa5 this/container/has/a/very/long/path/elasticsearch:6.8.0
为此:
43b15196a114 nginx:1.17.2-alpine
5cfb448d2aa5 th/elasticsearch:6.8.0
有 slice
功能,但有几个注意事项:
- 切片索引不应超过数组(字符串)范围。
.Image
字段是某种奇怪的类型(不是字符串),因此您应该将其评估为字符串(使用 print
)并为其分配一些名称。
docker container ls --format 'table {{.ID}}\t{{with $i:=(print .Image)}}{{if le (len $i) 20}}{{$i}}{{else}}{{slice $i (slice $i 20|len)}}{{en
d}}{{end}}'
我正在尝试编写一个 docker 格式化程序,使 docker container ls
的输出更具可读性。我想截断图像字段,以便只打印最后 n
个字符(我处理的一些图像有很长的路径)。
例如:如何修改 {{.Image}}
以便输出来自:
docker container ls --format "table {{.ID}}\t{{.Image}}"
43b15196a114 nginx:1.17.2-alpine
5cfb448d2aa5 this/container/has/a/very/long/path/elasticsearch:6.8.0
为此:
43b15196a114 nginx:1.17.2-alpine
5cfb448d2aa5 th/elasticsearch:6.8.0
有 slice
功能,但有几个注意事项:
- 切片索引不应超过数组(字符串)范围。
.Image
字段是某种奇怪的类型(不是字符串),因此您应该将其评估为字符串(使用print
)并为其分配一些名称。
docker container ls --format 'table {{.ID}}\t{{with $i:=(print .Image)}}{{if le (len $i) 20}}{{$i}}{{else}}{{slice $i (slice $i 20|len)}}{{en
d}}{{end}}'