如何正确地实时输出bash到文本文件?

时间:2014-04-26 11:59:44

标签: bash shell grep

我想这样做并将其保存到文本文件中说abc.txt。

top | grep "Cpu(s)"

我理解我可以在bash中做到这一点

top | stdbuf -o0 grep "Cpu(s)" > abc.txt

我输出正确但是当我在gedit中打开txt文件时,它看起来像这样:

Cpu(s): (B [m [39;49m (B [m  1.0% (B [m [39;49mus, (B [m [39;49m (B [m  0.3% (B [m [39;49msy, (B [m [39;49m (B [m  0.6% (B [m [39;49mni, (B [m [39;49m (B [m 97.9% (B [m [39;49mid, (B [m [39;49m (B [m  0.1% (B [m [39;49mwa, (B [m [39;49m (B [m  0.0% (B [m [39;49mhi, (B [m [39;49m (B [m  0.0% (B [m [39;49msi, (B [m [39;49m (B [m  0.0% (B [m [39;49mst (B [m [39;49m [K
Cpu(s): (B [m [39;49m (B [m  0.7% (B [m [39;49mus, (B [m [39;49m (B [m  1.0% (B [m [39;49msy, (B [m [39;49m (B [m  0.0% (B [m [39;49mni, (B [m [39;49m (B [m 98.3% (B [m [39;49mid, (B [m [39;49m (B [m  0.0% (B [m [39;49mwa, (B [m [39;49m (B [m  0.0% (B [m [39;49mhi, (B [m [39;49m (B [m  0.0% (B [m [39;49msi, (B [m [39;49m (B [m  0.0% (B [m [39;49mst (B [m [39;49m [K`

Instead of this:

    ​Cpu(s):  1.0%us,  0.3%sy,  0.6%ni, 97.9%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
    Cpu(s):  0.7%us,  1.0%sy,  0.0%ni, 98.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

1 个答案:

答案 0 :(得分:6)

使用top -b删除所有特殊显示字符:

top -b | grep "Cpu(s)" > abc.txt