首页 > 解决方案 > 将顶部输出重定向到文件时可以看到特殊字符

问题描述

我正在尝试重定向 top 命令的输出,但我可以在文件中看到额外的特殊字符。

top -n 10 | grep --color=auto -i influx  >> perf.txt

在文件中

(B[m21942 root      20   0  832268 286408  38792 S   5.9  0.9 478:07.97 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   8.3  0.9 478:08.22 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   0.7  0.9 478:08.25 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   3.0  0.9 478:08.34 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   0.3  0.9 478:08.35 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   0.3  0.9 478:08.36 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   3.3  0.9 478:08.46 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   0.3  0.9 478:08.47 influxd                                                                                                                                                               (B[m[39;49m[K
(B[m21942 root      20   0  832268 286408  38792 S   0.3  0.9 478:08.48 influxd                                                                                                                                                               (B[m[39;49m[K

我们如何避免上述特殊字符?

标签: linuxshellunix

解决方案


top命令具有批处理模式 (-b),它将从输出中消除光标控制序列。根据手册页,它可以更容易地解析其他程序的输出。

此外,考虑从 grep 中删除--color,这将引入控制序列以突出显示匹配项

top -b -n10 | grep influx >> perf.txt

推荐阅读