Search code examples
rbibtex

Merge many bib tex files into one


I have multiple single bibtex files which are like this:

first file:

@article{DBLP:journals/access/AlotaibiAASA20,
  author    = {Bashayer Alotaibi and
               Rabeeh Ayaz Abbasi and
               Muhammad Ahtisham Aslam and
               Kawther Saeedi and
               Dimah Alahmadi},
  title     = {Startup Initiative Response Analysis {(SIRA)} Framework for Analyzing
               Startup Initiatives on Twitter},
  journal   = {{IEEE} Access},
  volume    = {8},
  pages     = {10718--10730},
  year      = {2020},
  url       = {https://doi.org/10.1109/ACCESS.2020.2965181},
  doi       = {10.1109/ACCESS.2020.2965181},
  timestamp = {Fri, 07 Feb 2020 12:04:40 +0100},
  biburl    = {https://dblp.org/rec/journals/access/AlotaibiAASA20.bib},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

second file:

@inproceedings{DBLP:conf/comad/MathewKG020,
  author    = {Binny Mathew and
               Navish Kumar and
               Pawan Goyal and
               Animesh Mukherjee},
  editor    = {Rishiraj Saha Roy},
  title     = {Interaction dynamics between hate and counter users on Twitter},
  booktitle = {CoDS-COMAD 2020: 7th {ACM} {IKDD} CoDS and 25th COMAD, Hyderabad India,
               January 5-7, 2020},
  pages     = {116--124},
  publisher = {{ACM}},
  year      = {2020},
  url       = {https://doi.org/10.1145/3371158.3371172},
  doi       = {10.1145/3371158.3371172},
  timestamp = {Wed, 22 Jan 2020 14:37:05 +0100},
  biburl    = {https://dblp.org/rec/conf/comad/MathewKG020.bib},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

How is it possible to read them all (they are in the same path) and create a new file which will be just a paste of all of them.

Example of expected output

@article{DBLP:journals/access/AlotaibiAASA20,
    author    = {Bashayer Alotaibi and
                 Rabeeh Ayaz Abbasi and
                 Muhammad Ahtisham Aslam and
                 Kawther Saeedi and
                 Dimah Alahmadi},
    title     = {Startup Initiative Response Analysis {(SIRA)} Framework for Analyzing
                 Startup Initiatives on Twitter},
    journal   = {{IEEE} Access},
    volume    = {8},
    pages     = {10718--10730},
    year      = {2020},
    url       = {https://doi.org/10.1109/ACCESS.2020.2965181},
    doi       = {10.1109/ACCESS.2020.2965181},
    timestamp = {Fri, 07 Feb 2020 12:04:40 +0100},
    biburl    = {https://dblp.org/rec/journals/access/AlotaibiAASA20.bib},
    bibsource = {dblp computer science bibliography, https://dblp.org}
  }

  @inproceedings{DBLP:conf/comad/MathewKG020,
    author    = {Binny Mathew and
                 Navish Kumar and
                 Pawan Goyal and
                 Animesh Mukherjee},
    editor    = {Rishiraj Saha Roy},
    title     = {Interaction dynamics between hate and counter users on Twitter},
    booktitle = {CoDS-COMAD 2020: 7th {ACM} {IKDD} CoDS and 25th COMAD, Hyderabad India,
                 January 5-7, 2020},
    pages     = {116--124},
    publisher = {{ACM}},
    year      = {2020},
    url       = {https://doi.org/10.1145/3371158.3371172},
    doi       = {10.1145/3371158.3371172},
    timestamp = {Wed, 22 Jan 2020 14:37:05 +0100},
    biburl    = {https://dblp.org/rec/conf/comad/MathewKG020.bib},
    bibsource = {dblp computer science bibliography, https://dblp.org}
  }

Solution

  • Edited:

    I tested the code below and it merges multiple files into one:

    First, extract all paths to the .bib files ("." if they are in the working directory, "path/to/directory/" or "/absolute/path/to/directory" otherwise:

    path_to_bib_files <- list.files(".", pattern="\\.bib$", full.names=TRUE)
    

    Then, iterate through the files one by one and append them:

    combined_bib <- ""
    for (path_to_bib_file in path_to_bib_files) {
    
      fileCon <- file(path_to_bib_file)
      content <- readLines(fileCon)
      close(fileCon)
    
      combined_bib <- paste0(combined_bib, "\n", "\n", trimws(paste0(content, collapse="\n")))
    
    } 
    

    Finally, write the combined string to a file:

    cat(combined_bib, file="combined_references.bib", "\n")