In-place editing using sed on AIX
Solution 1
you can use perl
to do it:
perl -p -i.bak -e 's/old/new/g' test.txt
It's going to create a .bak
file.
Or without a .bak
file :
perl -pi -e 's/old/new/g' test.txt
Or install sed-4.1.1
RPM from here.
Solution 2
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.
Solution 3
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.
Related videos on Youtube
danielbmeireles
DevOps + Comic Books + Good Food and Drinks :)
Updated on September 18, 2022Comments
-
danielbmeireles over 1 year
Is there any undocumented flag in AIX's
sed
implementation that allows for in-place editing in the same way as with e.g. GNUsed
? The manual shows no flag for this operation, which is one of the most useful ones in othersed
implementations.-
terdon about 10 yearsIf you don't see it in the man page, you can be pretty sure it doesn't exist. Just use
sed 's/a/b/' foo > bar && mv bar foo
-
Stéphane Chazelas about 10 yearsUse
perl -pi
instead. -
terdon about 10 years@StephaneChazelas is Perl installed on AIX?
-
manatwork about 10 yearsJust one thing to note regarding @terdon's suggestion: that may result a foo file with different owner/permissions than the original one.
-
terdon about 10 years@manatwork so will
sed -i
(at least on my Debian). It works by creating a temp file in the backgroud and renaming it to the original file's name so it will be owned by whoever ran thesed
. -
Stéphane Chazelas about 10 years@terdon, it was installed the last time I used AIX 15 years ago (but then again it might not have come pre-installed on those machines) and had the
-i
option (which is where GNU and FreeBSDsed
got the inspiration from for their -i decades afterperl
). -
manatwork about 10 years@terdon, the GNU
sed
4.2.2 I tried before my previous comment sets the ownership and permissions of the new file to match the old one. -
terdon about 10 years@manatwork ok, very weird. The GNU
sed
4.2.2 I tried here changes the ownership of my test file (permissions 777, in a directory with 777) to my user. -
manatwork about 10 years@terdon, probably my test case was too simple. I processed a file owned by user manatwork as user root. So changing the ownership had no impediments.
-
MAQ about 8 years@terdon - yes, perl is most likely present on modern AIX systems ( I used it on AIX 6 and 7)
-
-
Stéphane Chazelas about 10 yearsWhy would it not be possible with GNU
sed
installed? -
S edwards about 10 yearswell the GNU provided by AIX not the one you can compile see also ==> stackoverflow.com/questions/7232797/…
-
Stéphane Chazelas about 10 years
-i
was added insed-3.95
(2002). IBM offers 3.0.2, 4.0.7 and 4.1.1 AFAICT. Unless you get the older one, you'll get -i. Also note that you can get packaged version of GNUsed
for AIX from other sources. -
S edwards about 10 yearsWell it means my admin system never updated since 2002 I guess. because even with AIX 6.1 I don't have the possibility to use that switch
-
vonbrand about 10 years@Kiwy, run away as fast as you possibly can. A system that hasn't been properly maintained for a dozen years... the mind boggles.
-
S edwards about 10 years@vonbrand AIX 6.1 has been release in 2007 so I guess -i is not available since 2002
-
terdon about 10 years@Kiwy you
-i
is from GNUsed
which you don't have installed. -
vonbrand about 8 yearsThat is just a convoluted way of doing what the comment by @terdon does. Better yet, grab the GNU sed and install that.
-
Jeff Schaller about 8 yearsoption #2 will most likely truncate 'file' before reading from it -- be careful!
-
MAQ about 8 years@Kiwy - there is also pre-build GNU sed package perzl.org/aix/index.php?n=Main.Sed
-
Jeff Schaller about 8 years@StéphaneChazelas on a fairly stock AIX7.1 machine I have access to, IBM's sed still does not support
-i
. I could not determine the sed version - it rolls up to the bos.rte.edit package. -
Stéphane Chazelas about 8 years@JeffSchaller, yes nobody said AIX sed supported
-i
, just that you could get GNUsed
for AIX. -
Jeff Schaller about 8 years@StéphaneChazelas I was reading between the lines of your comment from 2 years ago: "-i was added in sed-3.95 (2002). IBM offers 3.0.2, 4.0.7 and 4.1.1 AFAICT" the implication that you saw recent versions of AIX offering sed's that supported -i.
-
Mat M almost 6 years@JeffSchaller : no, because the subshell should end before print opens its fd.
-
Mat M almost 6 yearsUse
print
with ksh or you will lose the line feeds. -
Mat M almost 6 years@vonbrand : it avoids creating a temp file, and it is sometimes a requirement
-
mosvy about 5 yearsfwiw, that's not what
sed
is doing.sed
is redirecting the output to a temp file, copy through the permissions and finally rename the temp file to the original. That will change the file's inode and wipe some precious metadata but a) will not trash the file's content if thesed -i
command was interrupted or failed b) will not trash the file's content if more that onesed -i
was run on the same file at the same time. More details and ranting here. -
Kusalananda about 5 years@mosvy All true. The way I have arranged the commands in my answer will ensure that the original contents (at least) is still available in the temporary file if the
sed
fails.