# How to concatenate two (file) streams

**URL:** https://discourse.julialang.org/t/how-to-concatenate-two-file-streams/124082
**Category:** General Usage
**Created:** [December 22, 2024, 2:19am UTC](https://discourse.julialang.org/t/how-to-concatenate-two-file-streams/124082 "2024-12-22T02:19:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Abhro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhro/32/220753_2.png) [@Abhro](https://discourse.julialang.org/u/Abhro)
#### Post date: [December 22, 2024, 2:19am UTC](https://discourse.julialang.org/t/how-to-concatenate-two-file-streams/124082/1 "2024-12-22T02:19:00Z")

</div>

How to create a stream which sequentially reads from multiple other streams? This is analogous to `cat` in unix, but for arbitrary streams. In python, something like this might be implemented as:

```python
def cat_streams(s1, s2):
    for line in s1:
        yield line
    for line in s2:
        yield line

```

(or some improved version thereof)

But I’m not sure what it would look like in Julia. I know there are task channels which yield, but this is more of a file imitation thing rather than an async thing.

---

<div class="post-metadata">

### Author: ![Yuan-Ru-Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuan-ru-lin/32/46068_2.png) [@Yuan-Ru-Lin](https://discourse.julialang.org/u/Yuan-Ru-Lin)
#### Post date: [December 22, 2024, 6:37am UTC](https://discourse.julialang.org/t/how-to-concatenate-two-file-streams/124082/2 "2024-12-22T06:37:34Z")

</div>

With `file1` and `file2` set up as

```bash
$ echo "line1 in file1\nline2 in file1\nline3 in file1" > file1
$ echo "line1 in file2\nline2 in file2\nline3 in file2" > file2
$ cat file1 file2
line1 in file1
line2 in file1
line3 in file1
line1 in file2
line2 in file2
line3 in file2

```

you can use `Iterators.flatten` to concatenate two iterators given by `eachline`.

```julia-repl
julia> for line in Iterators.flatten([eachline("file1"), eachline("file2")])
           println(line)
       end
line1 in file1
line2 in file1
line3 in file1
line1 in file2
line2 in file2
line3 in file2

```
