1

我正在尝试从 Linux 拆分“ls -lrt”命令的输出。但它只占用一个空格作为分隔符。如果有两个空格,那么它将第二个空格作为值。所以我认为我需要将多个空间压制为一个。有人对此有任何想法吗?

> 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))
4

2 回答 2

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?"  
于 2014-06-10T22:21:49.513 回答
2

这将在指定文件的数据框中返回该信息:

file.info(list.files(pattern = "[.]rds$", ignore.case = TRUE))

或者如果我们知道扩展名是小写的:

file.info(Sys.glob("*.rds"))
于 2014-06-10T23:23:07.457 回答