How to prepend to a file (add at the top)

后端 未结 8 996
逝去的感伤
逝去的感伤 2020-12-20 13:12

Imagine you have a file

sink(\"example.txt\")
data.frame(a = runif(10), b = runif(10), c = runif(10))
sink()

and would want to add some hea

相关标签:
8条回答
  • 2020-12-20 13:14

    In any language there is ultimately only one solution. And that is to overwrite the whole file:

    contents = readAllOf("example.txt")
    
    overwrite("example.txt", header + contents )
    
    0 讨论(0)
  • 2020-12-20 13:18

    in R there is no need to work with an extra file. You can just do :

    writeLines(c(header,readLines(File)),File)
    

    Yet, using the linux shell seems the most optimal solution, as R is not famous for performant file reading and writing. Especially not since you have to read in the complete file first.

    Example :

    Lines <- c(
    "First line",
    "Second line",
    "Third line")
    File <- "test.txt"
    header <- "A line \nAnother line \nMore line \n\n"
    
    writeLines(Lines,File)
    readLines(File)    
    
    writeLines(c(header,readLines(File)),File)
    readLines(File)
    unlink(File)
    
    0 讨论(0)
  • 2020-12-20 13:18

    Using bash:

    $ cat > license << EOF
    > /* created on 31.3.2011 */
    > /* author */
    > /* other redundant information */
    > EOF
    $ sed -i '1i \\' example.txt
    $ sed -i '1 {
    >    r license
    >    d }' example.txt
    

    Don't know how to do it with one sed command (sed -i -e '1i \\' -e '1 { ... inserts after first line).

    0 讨论(0)
  • 2020-12-20 13:22

    You generally can't expand a file backwards with most filesystems.

    Normally, when you save a file, the existing data is completely overwritten. Even if you only change the first two lines of a 1,000,000 line file, the application will usually re-write the unchanged lines to disk when you hit save.

    For most file formats, any headers are fixed size, so it's not a problem to change them.

    There are also formats that are stream based; since the data is parsed from the stream and used to construct the document, it's possible for the stream to contain an instruction to insert some data at the beginning of the resulting document. These stream-based file formats are fairly complicated, though.

    0 讨论(0)
  • 2020-12-20 13:23

    In R, following the original question:

    df <- data.frame(a = runif(10), b = runif(10), c = runif(10))
    txt <- "# created on 31.3.2011 \n# author \n# other redundant information"
    write(txt, file = "example.txt") 
    write.table(df, file = "example.txt", row.names = FALSE, append = TRUE)
    

    The solution provided by Joris Meys does not work properly, overwrites the content, not appending the header to the Lines. A working version would be:

    Lines <- c("First line", "Second line", "Third line")
    File <- "test.txt"
    header <- "A line \nAnother line \nMore line \n\n"
    writeLines(Lines, File)
    txt <- c(header, readLines(File)) 
    writeLines(txt, File) # Option1
    readLines(File) 
    

    Instead of writeLines we could use too:

    write(txt, File) # Option 2
    cat(txt, sep="\n", File) # Option 3
    
    0 讨论(0)
  • 2020-12-20 13:36

    It is totally easy in the linux shell:

    echo 'your additional header here' >> tempfile
    cat example.tst >> tempfile
    mv tempfile example
    rm tempfile
    
    0 讨论(0)
提交回复
热议问题