Merge two text file

I am trying to merge two files. Are there any command like appending a text file 2 to the end of text file 1

open(file1, "a") do f
    write(f, read(file2))
end

?

4 Likes

This kind of thing maybe easier to do using unix commands (assuming that you’re on a unixy system).

Is it possible to do without read(file2) as that can be expensive?

how do you know what content to append to the file1 then?..

The content of file2 is already in file2. So just stitch them together using some OS provided function?

and how exactly you expect the OS to make a copy on disk? it’s not like you can do soft link on partial file content.

1 Like

You can append to file1 from file2, which would avoid needing to copy one of the files, but you can’t avoid reading the other one. Why don’t you just do cat file2 >> file1? That’s going to be as fast as anything. It’s vaguely plausible that a copy-on-write file system could use the existing blocks of two files to create a new file without any data copying, but that would only be possible if the first file’s size happened to be a multiple of the block size, which is not generally going to be the case. Operating systems do not have this kind of API in any case.

3 Likes

Ok. Sounds like it isn’t possible. Oh well.