2

On RHEL, I am able to utilize the sed -i command to replace a string within a file, however, on AIX, it seems like the same -i parameter cannot be used.

I have a file called abc.xml that contains the following contents:

<test0/>
<test1/>
<config/>
<test2/>

I am trying to use the following command:

sed -i "s+<config />+<config>3242352353242342424</config>+g" /etc/abc.xml

to replace the parameter within my abc.xml file to:

<test0/>
<test1/>
<config>3242352353242342424</config>
<test2/>

Is the above possible?

Kusalananda
  • 333,661
Help
  • 97
  • Instead of using -i option, redirect the output of your sed to a temporary file and then move that temp file to the original path. – Philippos Apr 27 '21 at 14:18
  • I've done the following, it seems to be working, does this look correct to you? sed 's++3242352353242342424+g' testfile.xml > abc.xml; mv abc.xml testfile.xml – Help Apr 27 '21 at 15:27

2 Answers2

1

I doubt that your sed command works on RedHat as the spacing in the regular expression wouldn't match in the example document that you are showing. Also, parsing or modifying a document written in a structured document format such as XML is at best hazardous with a line-oriented text editing tool like sed.

The second issue is your use of the non-standard -i option. The portable ways of doing in-place editing are described in the Q/A How can I achieve portability with sed -i (in-place editing)?

Assuming that the XML document is well-formed, you would normally use a command line XML parser to modify the contents of the document.

One such command line XML parser is xmlstarlet.

xmlstarlet ed -u '//config' -v 3242352353242342424 file.xml

This would set the value of all config nodes to the string 3242352353242342424 (this value will be properly encoded by xmlstarlet if needs be). Any previous value will be discarded, any attributes to the config nodes will be retained.

The modified document would be written to standard output, so you could redirect it into a new file. There is also an --inplace option that you would use after the sub-command ed on the command line.

xmlstarlet is known to work on both Linux and AIX.

Kusalananda
  • 333,661
  • Thank you very much for the swift and detailed response, I really appreciate it! I ran the command and received the following output on my AIX 7 machine: bash: xmlstarlet: command not found – Help Apr 27 '21 at 12:52
  • @user435061 Then it's likely available as a 3rd-party package. As I don't know exactly how AIX is managed, I unfortunately can't guide you through an installation of it. – Kusalananda Apr 27 '21 at 13:02
  • No problem, I'm restricted on the system I am working on and will not be able to utilize 3rd-party packages. Do you know of any other built-in mechanisms that can be utilized for this? – Help Apr 27 '21 at 13:44
  • @Help you ask the system administrators or your Change Board to install it for you – Chris Davies Mar 23 '23 at 22:35
0

AIX does not support the -i option. You must manually redirect the output.

I recommend not stomping the original file in the same command - just back it up first.

cp /etc/abc.xml /etc/abc.xml.bak
sed "s+<config />+<config>3242352353242342424</config>+g" /etc/abc.xml.bak \
    > /etc/abc.xml

Use man sed to view the options that are available for AIX. There are many other differences in the commands. The s command should work fine though.

sampi
  • 553
Barett
  • 101