C:\ test中有一个名为haha
的文件,haha
包含字符look for me
,在linux中,我可以搜索获取文件名。
find / -name "look for me"
我可以在xp os中使用某种R命令搜索文件吗?
如果我不知道包含字符look for me
的文件名是haha
,我该怎么办呢?
答案 0 :(得分:1)
或与plyr:
require(plyr) # uses plyr
textFiles<-list.files(pattern=".txt") # only looks at .txt file, you can change or omit
#alply reads each file and returns
# a list of filenames which pass the grep test
# and indicate the first line identified
mylist<-alply(textFiles,
1,
function(f){fline<-grep("LOOK FOR ME",readLines(f))
ifelse(fline>0,paste(f,fline,sep=" - line:"),NULL)
})
Filter(is.character,mylist) # gives you a list of all files containing the term
答案 1 :(得分:0)
此代码将在其中找到带有短语“haha”的文件名。然后检查字符串“寻找我”是否出现在其中的任何位置。这就是你想要的吗?
whichfile <- grep(
x = list.files(),
pattern = "haha",
value = TRUE
)
sum(
grepl(
x = readLines(whichfile),
pattern = 'look for me')
)