here is the data.txt:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND  
root         1  0.0  0.0   2280   728 ?        Ss   20:44   0:00 init [2]    
root         2  0.0  0.0      0     0 ?        S    20:44   0:00 [kthreadd]  
root       202  0.0  0.0      0     0 ?        S<   20:44   0:00 [ext4-dio-unwri  
root       334  0.0  0.1   2916  1452 ?        Ss   20:44   0:00 udevd --daemon  
how to read the data into a data.frame?
1.can not to decide separator
the last field is a problem,space can not be the separator,
init [2]   ,udevd --daemon  are the one field,can not be separated by space.
2.no fixed width
every line has different width.  
so ,how can i read the data.txt into a data.frame?
ps displays status information about processes, and optionally, the threads running under each process. By default, for each process that is associated with the user's terminal, ps displays the process ID (PID), TTY, processor time used (TIME), and name of the command (COMM).
Linux provides us a utility called ps for viewing information related with the processes on a system which stands as abbreviation for “Process Status”. ps command is used to list the currently running processes and their PIDs along with some other information depends on different options.
Under the -@ flag, the ps command displays the name of the workload partition in which the process is running. Specify the -@ flag with the wparname parameter to display the process information. (Z flag) The data page size of the process. Indicates that the process is operating in core memory.
I would do it like this:
library(stringr) # has a convenient function for splitting to a fixed length 
raw          <- system("ps aux", intern = TRUE)
fields       <- strsplit(raw[1], " +")[[1]]
ps           <- str_split_fixed(raw[-1], " +", n = length(fields))
colnames(ps) <- fields
                        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