在多个目录中的PDF中删除单词(Mac)

时间:2017-10-05 14:22:04

标签: python r bash shell grep

这是一个非常简单且应该很容易解决的问题。我有一个目录,其子目录包含许多(!)可搜索的PDF。我现在想在那些PFD中找到单词,并希望输出包含这些单词的行。我已经尝试了pdfgrep override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) guard let items = tabBar.items else { return } items[0].title = "Title0" items[1].title = "Title1" items[2].title = "Title2" items[3].title = "Title3" } 但它只给了我一个结果而不是全部。使用pdfgrep,grep,R或Python的解决方案会很棒。

2 个答案:

答案 0 :(得分:0)

这是一个基于pdftools包的解决方案。我把它包装成一个函数,在一个文件夹中查找一组单词。结果是一个data.table,它包含文件名,目录和与单词匹配的行列表。

主意

  1. 以递归方式列出该目录中的所有pdf文件
  2. 将每个pdf转换为保持相同布局的文本
  3. 使用grep每个文本进行搜索并提取匹配的行。
  4. 在data.table中执行所有这些操作以获得智能聚合布局
  5. library(pdftools)
    library(data.table)
    search_pdf_dir <- function(dir_path,words){
      ff <- list.files(dir_path,
          pattern = "pdf$",full.names = TRUE,recursive = TRUE)
      dx <- data.table(file=basename(ff),directory=dirname(ff))
      dx[,lines:={
        xx <- sapply(ff,function(file_name){
          xx <- suppressWarnings(pdf_text(file_name))
          res <- grep(paste(words,collapse="|"),xx,value=TRUE)
          if(length(res)>0) res
        })
    
      }]
      dx[!vapply(lines,is.null,TRUE)]
    }
    

    使用它

    res <- search_pdf_dir(dir_path_containing_pdf,c("0.28854"))
    

    结果是:

    Classes ‘data.table’ and 'data.frame':  3 obs. of  3 variables:
     $ file     : chr  
     $ directory: chr  
     $ lines    :List of 3
     - attr(*, ".internal.selfref")=<externalptr> 
    

答案 1 :(得分:0)

我的第一个代码是正确的 - 它只花了20分钟才能运行......