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
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.
Use top -b
to remove all special display characters:
top -b | grep "Cpu(s)" > abc.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With