获取某些模式的列表文件

时间:2017-10-20 16:26:31

标签: r grep

我有以下文件:

TAMF_Flight1.txt
TAMF_Flight1_profile.txt
TAMF_Flight2.txt
TAMF_Flight2_profile.txt
...
TAMF_Flight10.txt
TAMF_Flight10_profile.txt
TAMF_Flight11.txt
TAMF_Flight11_profile.txt

我应该在模式中加入什么才能获得:

TAMF_Flight1.txt
TAMF_Flight2.txt
...
TAMF_Flight10.txt
TAMF_Flight11.txt

或仅限:

TAMF_Flight1_profile.txt
TAMF_Flight2_profile.txt
...
TAMF_Flight10_profile.txt
TAMF_Flight11_profile.txt

1 个答案:

答案 0 :(得分:1)

myfiles <- c('TAMF_Flight1.txt',
             'TAMF_Flight1_profile.txt',
             'TAMF_Flight2.txt',
             'TAMF_Flight2_profile.txt')

grep( "TAMF_Flight[0-9]+[^_]", myfiles, value = TRUE)
# [1] "TAMF_Flight1.txt" "TAMF_Flight2.txt"

grep( "TAMF_Flight[0-9]+[_]", myfiles, value = TRUE)
# [1] "TAMF_Flight1_profile.txt" "TAMF_Flight2_profile.txt"

使用list.files()

中的模式
list.files( path = ".", pattern = "TAMF_Flight[0-9]+\\.txt")
list.files( path = ".", pattern = "TAMF_Flight[0-9]+[_]")