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 sed
implementations.

- 333,661
5 Answers
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.

- 333,661

- 9,534
-
-
well the GNU provided by AIX not the one you can compile see also ==> https://stackoverflow.com/questions/7232797/sed-on-aix-does-not-recognize-i-flag – Kiwy Jan 30 '14 at 14:43
-
2
-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. – Stéphane Chazelas Jan 30 '14 at 15:08 -
Well 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 – Kiwy Jan 30 '14 at 15:17
-
@Kiwy, run away as fast as you possibly can. A system that hasn't been properly maintained for a dozen years... the mind boggles. – vonbrand Jan 30 '14 at 15:23
-
@vonbrand AIX 6.1 has been release in 2007 so I guess -i is not available since 2002 – Kiwy Jan 30 '14 at 15:27
-
-
@Kiwy - there is also pre-build GNU sed package http://www.perzl.org/aix/index.php?n=Main.Sed – MAQ Mar 01 '16 at 18:00
-
@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. – Jeff Schaller Apr 20 '16 at 15:05 -
@JeffSchaller, yes nobody said AIX sed supported
-i
, just that you could get GNUsed
for AIX. – Stéphane Chazelas Apr 20 '16 at 15:22 -
1@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. – Jeff Schaller Apr 20 '16 at 15:25
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.

- 544,893

- 333,661
-
1fwiw, 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. – Mar 12 '19 at 14:15 -
1@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. – Kusalananda Mar 12 '19 at 14:40
Define a variable and use a subshell to execute
sed
and redirect to a file. Very important use"
(double quote) to protect the variable$value
:value=$(sed 's/old/new/g' file) && echo "$value" > file
Or
Execute
echo
with a subshell you going to execute 'sed' and redirect to a file:echo "$(sed 's/old/new/g' file)" > file
-
That is just a convoluted way of doing what the comment by @terdon does. Better yet, grab the GNU sed and install that. – vonbrand Mar 01 '16 at 14:53
-
option #2 will most likely truncate 'file' before reading from it -- be careful! – Jeff Schaller Mar 01 '16 at 14:55
-
@JeffSchaller : no, because the subshell should end before print opens its fd. – Mat M Jul 19 '18 at 12:53
-
-
@vonbrand : it avoids creating a temp file, and it is sometimes a requirement – Mat M Jul 19 '18 at 12:55
Or use a here doc with ed -s
.
Sample short script - this hand copied, so may have a typo but the code is correct.
#!/bin/ksh
echo "test heredoc by adding def to a line with abc"
ed -s ./foo <<\END
1,$p /abc/s/$/def/p w
q
.
END
echo DONE
Test file foo
:
This is the test file
qwerty
asdfg
AWEfgEDH
abc this line needs def at the end!!!
asfpgt
My sample output:
97
abc this line needs def at the end!!!def
100
DONE

- 333,661

- 1
- 1
sed 's/a/b/' foo > bar && mv bar foo
– terdon Jan 30 '14 at 14:19perl -pi
instead. – Stéphane Chazelas Jan 30 '14 at 14:41sed -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
. – terdon Jan 30 '14 at 15:06-i
option (which is where GNU and FreeBSDsed
got the inspiration from for their -i decades afterperl
). – Stéphane Chazelas Jan 30 '14 at 15:10sed
4.2.2 I tried before my previous comment sets the ownership and permissions of the new file to match the old one. – manatwork Jan 30 '14 at 15:30sed
4.2.2 I tried here changes the ownership of my test file (permissions 777, in a directory with 777) to my user. – terdon Jan 30 '14 at 15:37