sed Additional Options Delay Creation/Truncation of Files


Files written to with the w command are created/truncated before any commands are run.

$ sed 'w created-file' < /dev/null && ls created-file && rm created-file

From the standard:

Each wfile shall be created before processing begins. Implementations shall support at least ten wfile arguments in the script; the actual number (greater than or equal to 10) that is supported by the implementation is unspecified. The use of the wfile parameter shall cause that file to be initially created, if it does not exist, or shall replace the contents of an existing file.

BSD sed provides the -a option to delay creating/truncating files until they are written to with the w command.

$ if sed -a 'w created-file' < /dev/null && [ ! -e created-file ]; then
>       echo The file was not created
> fi
The file was not created