I have been saving the output of the Android command top into a text file using Python (Bare with me as this is an R inquiry).
Unfortunately, I was missing a parameter that would just save the output as parseable without any ASCII escape commands (See https://unix.stackexchange.com/questions/409053/how-to-disable-color-in-output-of-top-command). I was able to fix that problem using the suggestion in the link.
Since I discovered the fix recently, I have quite a few older non-processed files with these extra characters (see sample output below).
Copying part of the output from the actual text file:
[s[999C[999B[6n[u[H[J[?25l[H[J[s[999C[999B[6n[uTasks: 279 total, 5 running, 274 sleeping, 0 stopped, 0 zombie
Mem: 1702176K total, 1661708K used, 41439232 free, 11583488 buffers
Swap: 425540K total, 345512K used, 81948672 free, 487176K cached
400%cpu 138%user 3%nice 235%sys 5%idle 0%iow 0%irq 19%sirq 0%host
[7m PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS [0m
I have an R program that reads these text files and does some post processing which works fairly well MOST of the time. Other instances these extra characters in a given file require manual cleanup.
Is there a way that I could modify my read.table command to tell R to ignore these wherever they are found ?
My code to read the text file and store the output into a CSV file is below. There is additional post-processing after that, which is really not relevant to the ask:
for (file in data_files)
{
i = i + 1
ncol <- max(count.fields(paste(folder,file,sep="/"), sep = ""))
Top_Data_Frame <- read.table(paste(folder,file,sep="/"), header = FALSE, fill=TRUE, col.names=paste0('V', seq_len(ncol)))
write.csv(Top_Data_Frame, file = paste(folder,(paste((paste("The_Whole_File", i, sep="")),".csv", sep="")), sep="/"), row.names=FALSE)
Suggestions are appreciated.


Try this: