Re: shell script replacing original file

Re: shell script replacing original file

am 31.03.2008 18:11:34 von merlyn

>>>>> "pk" == pk writes:

pk> GNU sed does it this way:

pk> `-i[SUFFIX]'
pk> `--in-place[=SUFFIX]'
pk> This option specifies that files are to be edited in-place. GNU
pk> `sed' does this by creating a temporary file and sending output to
pk> this file rather than to the standard output.(1).

pk> This option implies `-s'.

pk> When the end of the file is reached, the temporary file is renamed
pk> to the output file's original name. The extension, if supplied,
pk> is used to modify the name of the old file before renaming the
pk> temporary file, thereby making a backup copy(2)).

pk> This rule is followed: if the extension doesn't contain a `*',
pk> then it is appended to the end of the current filename as a
pk> suffix; if the extension does contain one or more `*' characters,
pk> then _each_ asterisk is replaced with the current filename. This
pk> allows you to add a prefix to the backup file, instead of (or in
pk> addition to) a suffix, or even to place backup copies of the
pk> original files into another directory (provided the directory
pk> already exists).

pk> If no extension is supplied, the original file is overwritten
pk> without making a backup.

Very similar to the way Perl does it, but for historical reasons, Perl does
the rename on open, not on close, so a "temporary name" isn't needed.

Modern versions of Perl support the '*' convention as well.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!