sed In-Place Editing

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Syntax

  • sed -I extension - FreeBSD sed (continuous line-counter)
  • sed -I[extension] - NetBSD and Illumos sed (continuous line-counter)
  • sed -i extension - FreeBSD sed
  • sed -i[extension] - NetBSD, OpenBSD, Illumos, BusyBox and GNU sed
  • sed --in-place[=extension] - Illumos, BusyBox, and GNU sed

Parameters

ParameterDetails
extensionSave a backup file with the specified extension, or no backup file when extension is a zero-length string.

Remarks

In-place editing is a common but non-standard extension present in the majority of recent systems.

From a BSD sed manual

(a section like this appears in all current BSD sed manuals, and those of their derivatives)

It is not recommended to give a zero length extension when in place editing files, as it risks corruption or partial content in situations where disk space is exhausted, etc.

Don't forget the mighty ed

There is definitely a use for sed and for in-place editing features of sed, but when the UNIX standard is extended, we should always ask why the old UNIX standard did not include that feature. Though UNIX is not perfect, the orthogonality and completeness of the tools has been developed to be quite near to perfection, at least for purposes that where visible around 1970: Text editing and automated text editing was surely visible around that time.

Actually, the idea of sed is not to edit a file in place, but to edit a stream. That's why the name sed is a short form of stream editor. Take away the s, and you get the tool that was actually designed for file editing: ed:

printf 'g/what to replace/s//with what to replace/g\nw\nq\n' | ed file

or cat file_edit_commands | ed file.



Got any sed Question?