Sed on AIX does not recognize -i flag

Does it work sed -ion AIX?

If not, how can I edit the file in place on AIX?

+5
source share
6 answers

The parameter -iis a GNU extension (non-standard) for the command sed. This was not part of the classic interface sed.

You cannot edit in situ directly on AIX. You should make the equivalent of:

sed 's/this/that/' infile > tmp.$$
mv tmp.$$ infile

You can process only one file at a time, and the parameter -iallows you to get the result for each of the many files in the argument list. The option -isimply packs this sequence of events. This is undoubtedly useful, but it is not standard.

script, , , ; , . - :

tmp=tmp.$$      # Or an alternative mechanism for generating a temporary file name
for file in "$@"
do
    trap "rm -f $tmp; exit 1" 0 1 2 3 13 15
    sed 's/this/that/' $file > $tmp
    trap "" 0 1 2 3 13 15
    mv $tmp $file
done

, sed (HUP, INT, QUIT, PIPE TERM). sed , , mv.

, , , , , .

(sed 's/this/that' ) . !

overwrite (shell), " UNIX" .

+13

vi:

vi file >/dev/null 2>&1 <<@
:1,$ s/old/new/g
:wq
@

- vi-edit, ESC.
ESC CTRL-V ESC.
, vi , TERM . export TERM=vt100 vi.

0

Another option is to use the good old ed, for example:

ed fileToModify <<EOF
,s/^ff/gg/
w
q
EOF
0
source

you can use perl for this:

perl -p -i.bak -e 's/old/new/g' test.txt

going to create a .bak file.

0
source
#!/bin/ksh
host_name=$1
perl -pi -e "s/#workerid#/$host_name/g" test.conf 

Above, # workerid # will be replaced with $ host_name inside test.conf

0
source

All Articles