打印到文件时没有清晰的字符

打印到文件时没有清晰的字符

我想将 docker stats 的输出打印到文件中。

例如,我正在运行:

docker stats --format "{{ .Name }},{{ .MemUsage }},{{ .MemPerc }},{{ .CPUPerc }}" > /home/test.txt

但是,由于 docker stats 的正常输出位于已更新的一行上,因此在文件中我打印出了清晰的字符 (^[[3J^[[H^[[2J)]。

如何在没有该字符的情况下打印输出?

我还附上一张图片以清楚地表明我的输出内容。

在此输入图像描述

答案1

你可以通过管道它抗过滤器:

docker stats --format "{{ .Name }},{{ .MemUsage }},{{ .MemPerc }},{{ .CPUPerc }}"  | ansifilter > /home/test.txt

请注意,它将/home/test.txt包含多行。它看起来像这样:

alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,656KiB / 7.476GiB,0.01%,0.00%
alpine,528KiB / 7.476GiB,0.01%,0.00%
alpine,528KiB / 7.476GiB,0.01%,0.00%
alpine,528KiB / 7.476GiB,0.01%,0.03%
alpine,528KiB / 7.476GiB,0.01%,0.03%
alpine,528KiB / 7.476GiB,0.01%,0.02%

相关内容