I ran these two code blocks, expecting the same output
cattest <- file("cattest.txt")
cat("First thing", file = cattest)
cat("Second thing", file = cattest, append = TRUE)
close(cattest)
sink("cattest_sink.txt")
cat("First thing")
cat("Second thing")
sink()
But the resulting cattest.txt
contains only "Second thing", whereas the cattest_sink.txt
includes what I expected, "First thingSecond thing". Why is the append
argument ignored with the file connection?
I'm on 64bit R 3.0.1 on Windows, in case it matters.
You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.
You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems.
The tee command copies the text from standard input and writes it to the standard output file. The tee provides -a option to append the text to the file.
The key takeaway from this section is that we use cat > FILENAME to create or overwrite a file. Additionally, we can use cat >> FILENAME to append to a file that's already there. Then after typing in the text we want we use CTRL + D to exit the editor, return to the command line, and create the file.
Because that's what ?cat
says it will do if file
is not the name of a file.
append: logical. Only used if the argument 'file' is the name of file (and not a connection or '"|cmd"'). If 'TRUE' output will be appended to 'file'; otherwise, it will overwrite the contents of 'file'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With