3

I can read a file and find a special line based on a predicate like this

open(file, read=true, write=true, append=true) do io
    for line in eachline(io)
        if predicate(line)
            new_line = modifier(line)
            # how to replace the line with new_line in the file now?
        end
    end
end

But how can I change the content in the file now?

1 Answer 1

5

In general, you can't modify a file in-place (this is true for languages other than julia too), because adding or removing characters alters the positions of everything that comes after (a file is just one long string of bytes).

So you can either

  1. read in your whole file, change what you want to change, and then write the whole file to the same location
  2. read line-by-line, write to a new location, and then copy the new file to the old location

The latter is probably better in case you have really giant files (so you don't have to store the whole thing in memory). You've got most of the code, it's just a matter of creating a temp file, and then copying back to the original path at the end. Something like:

(tmppath, tmpio) = mktemp()
open(file) do io
    for line in eachline(io, keep=true) # keep so the new line isn't chomped
        if predicate(line)
            line = modifier(line)
        end
        write(tmpio, line)
    end
end
close(tmpio)
mv(tmppath, file, force=true)

NOTE: if this is at the global scope (eg, not inside a function), you might have to put global in front of tmpio inside the do block. Alternatively, wrap the whole thing in let. See here.

4
  • 2
    this needs mv(tmppath, file, force = true) since file exists already.
    – Lufu
    Commented Sep 27, 2019 at 15:36
  • 2
    and a close(tmpio) before mv
    – Lufu
    Commented Sep 27, 2019 at 15:48
  • Also either write(tmpio, line) should be write(tmpio, line * "\n") or eachline(io) should be eachline(io, keep=true). Otherwise, all lines are concatenated.
    – Dominique
    Commented Feb 27, 2020 at 23:33
  • @Dominique :+1: fixed
    – kevbonham
    Commented Mar 1, 2020 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.