如何在R中读取linux进程状态(ps)命令的输出? [英] How to read output from linux process status (ps) command in R?
本文介绍了如何在R中读取linux进程状态(ps)命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是data.txt:
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
如何将数据读入data.frame ?
1.无法决定分隔符
最后一个字段是个问题,空格
不能是分隔符,< br>
init [2],udevd --daemon是一个字段,不能用空格
分隔。
2.no固定宽度
每行都有不同的宽度。
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.
那么,我如何将data.txt读入data.frame?
so ,how can i read the data.txt into a data.frame?
推荐答案
我会这样做:
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
这篇关于如何在R中读取linux进程状态(ps)命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文