# Removing the first line in a text file

**URL:** <https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 4, 2020, 2:39pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521 "2020-03-04T14:39:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [March 4, 2020, 2:39pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/1 "2020-03-04T14:39:59Z")

</div>

Hi everyone,

how would I go about removing the first line of a (potentially very large) text file, ideally without having to load the file into a DataFrame or copying the full file?

Best  
Jakob

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 4, 2020, 2:56pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/2 "2020-03-04T14:56:29Z")

</div>

Check out [working with text files](https://en.wikibooks.org/wiki/Introducing_Julia/Working_with_text_files) from the Julia wikibook.

```julia
open("file_to_read") do f
       io = open("test.txt", "w")
       i = 1
       for l in eachline(f)
          i != 1 && println(io, l) 
          i += 1
       end
       close(io)
end

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 4, 2020, 3:08pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/3 "2020-03-04T15:08:14Z")

</div>

Do you just want to save it back on your disk in-place? It’d be nice to just tell the filesystem that the file starts at a new place, but I don’t think that’s a supported operation on any system and would likely have to be removed in exact multiples of 1 or 2k. I think the only way to do it is to work through the entire file and re-write it.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 4, 2020, 3:13pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/4 "2020-03-04T15:13:18Z")

</div>

Do you need to write a program? I mean if you are on linux you would do:

```julia
   tail -n +2 input.txt > output.txt

```

Not sure what the Window’s equivalent would be…I’d probably install the Ubuntu “App” to get a bash prompt with tail, and run from in there. 🙂

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 4, 2020, 3:14pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/5 "2020-03-04T15:14:44Z")

</div>

Note that this adds an unconditional new line at the end, which may or may not be OK.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 4, 2020, 6:05pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/6 "2020-03-04T18:05:41Z")

</div>

Even simpler:

```julia
open("file_to_read") do input
    open("test.txt", "w") do output
        for line in Iterators.drop(eachline(input), 1)
            println(output, line)
        end
    end
end

```

If you want to maximize speed, and avoid the extraneous newline pointed out by @yuyichao, it would be faster to read everything after the newline in a block:

```julia
open("file_to_read") do input
    readuntil(input, '\n')
    write("file_to_write", read(input))
end

```

This implementation is even shorter than the code based on `eachline`. (And it will be vastly faster than spawning a Unix program like `tail`, not to mention being more portable.)

The only downside of this approach is that it might take a lot of memory if you have an enormous file. A more general implementation would probably read the data in large blocks, similar [to this code](https://discourse.julialang.org/t/how-to-obtain-the-result-of-a-diff-between-2-files-in-a-loop/23784/4).

---

<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:** [March 4, 2020, 8:04pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/7 "2020-03-04T20:04:54Z")

</div>

why not use POSIX tools…  
[https://superuser.com/questions/284258/remove-first-line-in-bash](https://superuser.com/questions/284258/remove-first-line-in-bash)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 4, 2020, 9:53pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/8 "2020-03-04T21:53:47Z")

</div>

> [@jling](#):
>
> why not use POSIX tools…

POSIX tools are great, but Julia code that relies on them will not be portable.

Also, spawning executables takes a lot of time, so for simple tasks it is often [orders of magnitude faster](https://discourse.julialang.org/t/how-to-obtain-the-result-of-a-diff-between-2-files-in-a-loop/23784/4) to use Julia code than to spawn a POSIX command-line program.

For example, `run(pipeline(`tail -n +2 $inputname`, stdout=outputname))` seems to be about 10²× slower on my computer than my native-Julia `readuntil` code [above](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/6) for most files.

---

<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:** [March 4, 2020, 10:40pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/9 "2020-03-04T22:40:16Z")

</div>

I agree with everything you said it’s just that op didn’t say they need programmatically do this multiple times on different occasions, so I thought I’d mention sed in case it’s just a one time thing.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 4, 2020, 11:51pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/10 "2020-03-04T23:51:16Z")

</div>

If you are generating the files in Julia then the most efficient solution would be to create a structure where you’ve implemented the IO methods for. That way you can filter out the first line and write the rest of the data.

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [March 5, 2020, 9:10am UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/11 "2020-03-05T09:10:01Z")

</div>

This is really neat! (There is an extra `)` though after `"file_to_write"` 🙂 )  
One can even point `"file_to_read"` and `"file_to_write`" to the same file to do the in-place replacement. As I’ve changed the workflow to cutting large files into smaller chunks, memory is not really an issue anymore.

Thanks for all the replies btw., julia really does have an awesome community!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 5, 2020, 3:24pm UTC](https://discourse.julialang.org/t/removing-the-first-line-in-a-text-file/35521/12 "2020-03-05T15:24:12Z")

</div>

> [@Jakob](#):
>
> One can even point `"file_to_read"` and `"file_to_write` " to the same file to do the in-place

If you want the option to do this in-place, I would change the code to:

```julia
write("file_to_write", 
    open("file_to_read") do input
        readuntil(input, '\n')
        read(input)
    end)

```

so that the file is closed after reading before opening it to write.
