I know that > file
is not sed
specific, and that it writes stdout
to the file, but how does this compare to sed
's -i
option? I read the man page, and I do not understand what in-place
means. Are either of these choices definitively better? Which is faster? Takes less ram? If you have any additional insights, please share.

- 169
2 Answers
It all depends what you want to do - but pipes are generally very fast and just uses IO buffers.
-i is a non-standard option to edit files in place. sed otherwise just transforms the data on standard in to standard out
On my machine I can't notice any speed difference on a 1mb file.
$ cat my_file.txt
This is my file
$ sed -i "" s/my/your/g my_file.txt
$ cat my_file.txt
This is your file
$
"In-place" means the operation uses space (like diskspace or memory) already taken, no additional resource is needed. In this context it's about sticking to the provided file
.
The main advantage of sed -i -e '…' file
is you can read file
and have the output in a file still named file
. It looks like the same file, but behind the scenes it's not. By default, sed
reads the given file, processes it outputting into a temporary file, then moves the temporary file to the original name, replacing the original file.
So it's not strictly "in-place" because another file is needed and at some point you have two files; but the result is almost as if it was in-place. Writing to a separate file has its benefits:
The old version of the file is atomically replaced by the new version, so at every point in time the file name points to a valid, complete version of the file.
On the other hand sed -e '…' file >file
doesn't work. Worse, it makes file
lose its content. The behavior is explained here: Bending a pipeline back into its origin. If you try to achieve with >
what -i
does then you will most likely lose data.

- 21,864
>
what-i
does then you will most likely lose data." This is important to note (I learned it the hard way). – Not me Jun 04 '20 at 22:52