Is there any undocumented flag in AIX's
sed implementation that allows for in-place editing in the same way as with e.g. GNU
sed? The manual shows no flag for this operation, which is one of the most useful ones in other
you can use
perl to do it:
perl -p -i.bak -e 's/old/new/g' test.txt
It's going to create a
Or without a
.bak file :
perl -pi -e 's/old/new/g' test.txt
sed-4.1.1 RPM from here.
This is not possible on AIX even with the
sed tool installed.
You do need to use a temp file like suggested by terdon in comments to the question:
sed 's/a/b/' foo > bar && mv bar foo
You could also use
ed which does inline editing.
To use a standard compliant
sed, which does not have
-i, to do in-place editing, in such a way that permissions, file ownerships and some other metadata are not modified and hard links and symlinks are not broken:
cp file file.tmp && sed 'expression' file.tmp >file && rm -f file.tmp
This creates a temporary copy of the file we want to edit, and then applies the
sed command to this copy while redirecting the result to the original filename.
Redirecting to the original filename will truncate and rewrite that file, and since the file is not first deleted and recreated, it will retain most of its metadata.
Define a variable and use a subshell to execute
sedand redirect to a file. Very important use
"(double quote) to protect the variable
value=$(sed 's/old/new/g' file) && echo "$value" > file
echowith a subshell you going to execute 'sed' and redirect to a file:
echo "$(sed 's/old/new/g' file)" > file