I am trying to substitute few special characters using sed/perl for which I am getting errors.
In below command i am trying to replace values for SEARCHFIELDAPP, OLD_STRING,NEW_STRING etc;
Server1> SERVER_COMMAND="perl -F'\|' -i -lape 'if (\$F[0] eq \"SEARCHFIELDAPP\" && \$F[1] eq \"SEARCHFIELDDB\" && \$F[6] eq \"SEARCHFIELDUNM\" ) { s/\|OLD_STRING\|/\|NEW_STRING\|/g; s/\|LAST_UPDATE_DT/\|CURR_UPDATE_DT/g }' "
Above command is as shown below when printed on console.
Server1> echo $SERVER_COMMAND
perl -F'\|' -i -lape 'if ($F[0] eq "SEARCHFIELDAPP" && $F[1] eq "SEARCHFIELDDB" && $F[6] eq "SEARCHFIELDUNM" ) { s/\|OLD_STRING\|/\|NEW_STRING\|/g; s/\|LAST_UPDATE_DT/\|CURR_UPDATE_DT/g }'
In above command string, I am trying to replace few Strings:
Server1> echo $SERVER_COMMAND | sed -e "s|SEARCHFIELDAPP|ALLOWSERVICE|;s|SEARCHFIELDDB|DataDup|;s|SEARCHFIELDUNM|mogga|;s|OLD_STRING|mogga_dev_$%^|;s|NEW_STRING|mogga&*%^$|;s|LAST_UPDATE_DT|09-21-2012:11:09:41|;s|CURR_UPDATE_DT|09-21-2012:11:18:17|;"
When I do that I am getting an output as shown below:
perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_$%^\|/\|moggaNEW_STRING*%^$\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'
Problem here is with substitution s|NEW_STRING|mogga&*%^$|; . I was expecting NEW_STRING to be replaced with mogga&*%^$ but it is getting replaced with moggaNEW_STRING*%^$.
Here & is getting interpreted as NEW_STRING. When I escape &, it works fine. But I get this string from a parent script which i use in child script dynamically. I am looking for some way to successfully substitute string as it is, without escaping any character. Escaping will make me to modify incoming string with escape character which i want to avoid.
I tried the same by using perl one liner for substitution. But here also i am facing similar issue with special character.
Server1> echo $SERVER_COMMAND | perl -p -i -e 's|SEARCHFIELDAPP|ALLOWSERVICE|;s|SEARCHFIELDDB|DataDup|;s|SEARCHFIELDUNM|mogga|;s|OLD_STRING|mogga_dev_$%^|;s|NEW_STRING|mogga&*%^|;s|LAST_UPDATE_DT|09-21-2012:11:09:41|;s|CURR_UPDATE_DT|09-21-2012:11:18:17|;'
Below is how it looks after substitution:
perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_0^\|/\|mogga&*%^\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'
I am seeing string OLD_STRING which was suppose to get replaced with string mogga_dev_$%^ got replaced with mogga_dev_0^. Here $% is getting converted to 0
In this case as well i get what expect by escaping problematic characters.
Can you please let me know how to avoid this interpretation apart from escaping special characters?
Thanks
After William's inputs I was able to achieve below without errors:
perl -F'\|' -i -lape 'if ($F[0] eq "ALLOWSERVICE" && $F[1] eq "DataDup" && $F[6] eq "mogga" ) { s/\|mogga_dev_!$%^\|/\|mogga!&*%^$@*&^%\|/g; s/\|09-21-2012:11:09:41/\|09-21-2012:11:18:17/g }'
But when I execute this on a file for substitution. I am not seeing string mogga_dev_!$%^ is not getting replaced with mogga!&%^$@&^% in the file.
Since special characters are not escaped, I think replacement is not happening. But I dont want to use escaping to escape special characters. Can you please help me with some other method in replacing string with special characters in file?
Thanks