Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output bash to text file in real time correctly?

Tags:

grep

bash

shell

I want to do this and save this to a text file say abc.txt.

top | grep "Cpu(s)"

I usderstand I can do this in bash

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

I get my output correctly but when I open the txt file in gedit, it looks like this:

​​

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
like image 472
slimlambda Avatar asked Apr 26 '14 11:04

slimlambda


People also ask

How do I redirect a output to a file in bash?

For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected. “>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents.


1 Answers

Use top -b to remove all special display characters:

top -b | grep "Cpu(s)" > abc.txt
like image 86
anubhava Avatar answered Nov 15 '22 05:11

anubhava