How using sed on AIX differs from using it on linux

I know that -i option doesn't exist in sed for AIX but recently I've bumped into a different issue when trying to edit the /etc/services file in AIX.

I need to replace a line from this file so for example instead of this line:

nimhttp 4901/udp

I should have this one:

nimhttp 5001/tcp

On linux I could just use this barberian command:

sed 's/nimhttp 4901\/udp/nimhttp 5001\/tcp/g' /etc/services > /etc/services.tmp

Or maybe I could be little bit smarter and use this one:

sed 's/^nimhttp\s\{3,\}4901\/udp/nimhttp\s\{9\}5001\/tcp/g' /etc/services > /etc/services.tmp

And still end up with my .tmp file being edited as needed.

On AIX none of those (and many other more) commands I've tried do not work. Tried to figure out what are the differences between them but as sed is itself a complex command, I cannot.

So my question is, does anyone know why this doesn't work on AIX or can I find a list of differences between the two versions of the sed command found on both systems?

The linux sed command version is 4.2.2 and AIX's is unknown as sed --version doesn't work either nor is there a mention in sed's man page about finding the version but I assume it's also the latest as I'm on AIX 7.2

6

1 Answer

As said by Lorinczy Zsigmond, Linux versions of sed implements a lot of GNU Extensions to ease its use.

To test under Linux, use sed --posix (and welcome to the old Unix world ;-p)

To the job you want to achieve in "posix" mode, the following two commands works:

/tmp/test$ cat test_file
nimhttp 4901/udp
/tmp/test$ sed --posix -e 's/4901\/udp/5001\/tcp/g' test_file
nimhttp 5001/tcp
/tmp/test$ sed --posix -e 's#4901/udp#5001/tcp#g' test_file
nimhttp 5001/tcp

If you want to also match on the service name:

/tmp/test$ cat test_file
nimhttp 4901/udp
nimnothttp 104901/udp
/tmp/test$ sed --posix -e 's#\(nimhttp.*\)4901/udp#\15001/tcp#g' test_file
nimhttp 5001/tcp
nimnothttp 104901/udp

Note that \( \) is for memorizing matched pattern. Then you recall the memorized pattern witn \1 (or \2, \3 etc... depending on the number of memorized patterns).

Maybe the command you used to use won't work ... but like with Perl, there's always several ways to do the same things with sed.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like