我正在尝试拆分" ls -lrt"的输出。来自Linux的命令。但它只占用了一个空间作为分隔符。如果有两个空格则将第二个空格作为值。所以我认为我需要将多个空间压缩为一个。有没有人对此有任何想法?
> a <- try(system("ls -lrt | grep -i .rds", intern = TRUE))
> a
[1] "-rw-r--r-- 1 u7x9573 sashare 2297 Jun 9 16:10 abcde.RDS"
[2] "-rw-r--r-- 1 u7x9573 sashare 86704 Jun 9 16:10 InputSource2.rds"
> str(a)
chr [1:6] "-rw-r--r-- 1 u7x9573 sashare 2297 Jun 9 16:10 abcde.RDS" ...
>
>c = strsplit(a," ")
>c
[[1]]
[1] "-rw-r--r--" "1" "u7x9573" "sashare" ""
[6] "2297" "Jun" "" "9" "16:10"
[11] "abcde.RDS"
[[2]]
[1] "-rw-r--r--" "1" "u7x9573" "sashare"
[5] "86704" "Jun" "" "9"
[9] "16:10" "InputSource2.rds"
在下一步中我只需要文件名,我使用了以下代码,这些代码运行良好:
mtrl_name <- try(system("ls | grep -i .rds", intern = TRUE))
答案 0 :(得分:2)
strsplit采用正则表达式,因此我们可以使用它们来帮助。有关详细信息,请阅读?regex
> x <- "Spaces everywhere right? "
> # Not what we want
> strsplit(x, " ")
[[1]]
[1] "Spaces" "" "" "everywhere" "right?"
[6] ""
> # Use " +" to tell it to split on 1 or more space
> strsplit(x, " +")
[[1]]
[1] "Spaces" "everywhere" "right?"
> # If we want to be more explicit and catch the possibility of tabs, new lines, ...
> strsplit(x, "[[:space:]]+")
[[1]]
[1] "Spaces" "everywhere" "right?"
答案 1 :(得分:2)
这将在指定文件的数据框中返回该信息:
file.info(list.files(pattern = "[.]rds$", ignore.case = TRUE))
或者如果我们知道扩展名是小写的话:
file.info(Sys.glob("*.rds"))