# Merge two text file

**URL:** <https://discourse.julialang.org/t/merge-two-text-file/2280>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 24, 2017, 5:35pm UTC](https://discourse.julialang.org/t/merge-two-text-file/2280 "2017-02-24T17:35:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![schalil](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@schalil](https://discourse.julialang.org/u/schalil)\
**Post date:** [February 24, 2017, 5:35pm UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/1 "2017-02-24T17:35:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [February 24, 2017, 5:41pm UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/2 "2017-02-24T17:41:06Z")

</div>

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

```

?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 24, 2017, 9:40pm UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/3 "2017-02-24T21:40:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 3:02am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/4 "2019-09-06T03:02:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 6, 2019, 10:12am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/5 "2019-09-06T10:12:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 10:19am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/6 "2019-09-06T10:19:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 7, 2019, 12:20am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/7 "2019-09-07T00:20:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 7, 2019, 12:35am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/8 "2019-09-07T00:35:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 7, 2019, 12:58am UTC](https://discourse.julialang.org/t/merge-two-text-file/2280/9 "2019-09-07T00:58:37Z")

</div>

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