1
votes

I need to run a sed command via command through Informatica. I already know that Informatica executes commands via sh -c "{command}". However for my case, I need sed to replace a particular string such as sed -i 's,xsi:namespace="http://url.example.com",,' file.xml.

I have tried escaping the double-quotes with \ but no dice, even thought it works when I do it directly in Linux.

2

2 Answers

1
votes

Thanks for Jonathan Leffler's comment. I tested the command on Mac. See Jonathan's comments for more information.

Try this, it works for me

#!/bin/bash
sed -i '' -e  's,xsi:namespace="http://url.example.com",,' $1

Here's my test and output:

$ cat file.xml
xsi:namespace="http://url.example.com" hello there

$ cat test.sh
#!/bin/bash
sed -i '' -e  's,xsi:namespace="http://url.example.com",,' $1

$ ./test.sh file.xml      

$ cat file.xml
 hello there
0
votes

It is really tricky. One version that seems to work uses triple backslashes:

$ bash -c "sed 's,xsi:namespace=\\\"http://url.example.com\\\",,'" \
> <<< 'abc-xsi:namespace="http://url.example.com"-def'
abc--def
$

The first pair represent a backslash in the -c script; the backslash-quote represents a double quote in the -c script. You can add printf "%s\n" before the bash to see the arguments passed to bash:

$ printf "%s\n" bash -c "sed 's,xsi:namespace=\\\"http://url.example.com\\\",,'" \
> <<< 'abc-xsi:namespace="http://url.example.com"-def'
bash
-c
sed 's,xsi:namespace=\"http://url.example.com\",,'
$

However, a little cogitation and experimentation shows that single backslashes are sufficient:

$ printf "%s\n" bash -c "sed 's,xsi:namespace=\"http://url.example.com\",,'" <<< 'abc-xsi:namespace="http://url.example.com"-def'
bash
-c
sed 's,xsi:namespace="http://url.example.com",,'
$ bash -c "sed 's,xsi:namespace=\"http://url.example.com\",,'" <<< 'abc-xsi:namespace="http://url.example.com"-def'
abc--def
$

However, it's hard to see how you didn't try this when you tried single backslashes.

Note that double backslashes are not the answer:

$ bash -c "sed 's,xsi:namespace=\\"http://url.example.com\\",,'" <<< 'abc-xsi:namespace="http://url.example.com"-def'
sed: 1: "s,xsi:namespace=\http:/ ...": unterminated substitute in regular expression
$ printf "%s\n" bash -c "sed 's,xsi:namespace=\\"http://url.example.com\\",,'" <<< 'abc-xsi:namespace="http://url.example.com"-def'
bash
-c
sed 's,xsi:namespace=\http://url.example.com\,,'
$

Another alternative, well worth thinking about (and it's basically what haifzhan suggests in his answer), is to put the sed script — or the whole sed command — into a script that you run. This avoids questions of 'how many backslashes':

$ cat script.sed
s,xsi:namespace="http://url.example.com",,
$ sed -f script.sed <<<'abc-xsi:namespace="http://url.example.com"-def'
abc--def
$ bash -c "sed -f script.sed" <<<'abc-xsi:namespace="http://url.example.com"-def'
abc--def
$

The downside may be problems getting script.sed created and/or cleaned up.