Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't `cat` append to a `file` connection?

Tags:

r

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.

like image 813
Gregor Thomas Avatar asked Jul 17 '13 21:07

Gregor Thomas


People also ask

How do I append a file to a cat?

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>.

Is it possible to append contents using cat command?

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.

Can tee append to file?

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.

Does cat append or overwrite?

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.


1 Answers

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'.

like image 181
Joshua Ulrich Avatar answered Sep 28 '22 11:09

Joshua Ulrich