1
在C:\ test中有一個名爲haha
的文件,haha
包含字符look for me
,在linux中,我可以搜索以獲取文件名。搜索目錄以獲取文件名在R
find/ -name "look for me"
我可以使用某種R命令在xp os中搜索文件嗎?
如果我不知道包含字符look for me
的文件名是haha
,那我該怎麼辦?
在C:\ test中有一個名爲haha
的文件,haha
包含字符look for me
,在linux中,我可以搜索以獲取文件名。搜索目錄以獲取文件名在R
find/ -name "look for me"
我可以使用某種R命令在xp os中搜索文件嗎?
如果我不知道包含字符look for me
的文件名是haha
,那我該怎麼辦?
或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
這段代碼會在裏面找到一個帶有短語'haha'的文件名。然後檢查字符串「尋找我」是否發生在它的任何地方。那是你要的嗎?
whichfile <- grep(
x = list.files(),
pattern = "haha",
value = TRUE
)
sum(
grepl(
x = readLines(whichfile),
pattern = 'look for me')
)
你的問題是這麼寫的不好。我猜你有3個問題:1.如何找到名爲'haha'的文件。 2.如何檢查一個文件是否包含字符串'look for me'。 3.如何查找包含字符串'look for me'的所有文件。 – janos
@janos,我想OP是說文件的名字是未知的,但它包含字符串「尋找我」。 OP想要恢復文件名。 – Jota
弗蘭克,你是對的。 –