# Eachline function and more than one line

**URL:** <https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205>\
**Category:** New to Julia\
**Tags:** io\
**Created:** [April 19, 2025, 6:46am UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205 "2025-04-19T06:46:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 19, 2025, 6:46am UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205/1 "2025-04-19T06:46:40Z")

</div>

Hello,  
Can I use the `eachline` function to read multiple lines?

Thank you.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [April 19, 2025, 6:56am UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205/2 "2025-04-19T06:56:35Z")

</div>

Yes. `eachline` actually iterates over all lines in the file. So either do:

```julia
for line in eachline(file)
    # do something to the line
end

```

Or use `collect` to gather all lines:

```julia
collect(eachline(file)) # gives Vector{String} containing all lines

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [April 19, 2025, 7:56am UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205/3 "2025-04-19T07:56:00Z")

</div>

> [@abraemer](#):
>
> Or use `collect` to gather all lines:

Or just use `readlines` 🙂

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [April 19, 2025, 9:00pm UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205/4 "2025-04-19T21:00:44Z")

</div>

What the other replies have given are the most common ways to use `eachline`. If for some reason you actually have a use case where you need to read the file a specific number of multiple lines (eg. 3 lines) at a time, you can apply `first` to the iterator that `eachline` returns, and give it the number of lines to read. For eg.:

```julia
let e = eachline("./myfile.txt"; keep=true)
  while true
    # read (upto) 3 lines from the file
    lns = first(e, 3)
    # if we've already read the whole file, exit
    isempty(lns) && break

    #do something with the lines we read
    println(join(lns))
  end
end

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 20, 2025, 6:24am UTC](https://discourse.julialang.org/t/eachline-function-and-more-than-one-line/128205/5 "2025-04-20T06:24:23Z")

</div>

Hello,  
Thank you so much.
