0

I have the following two strings saved as variables:

str1='ds-cfg-global-aci: (targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10.2 || 1.3.6.1.4.1.42.2.27.8.5.1 || 2.16.840.1.113730.3.4.16 || 2.16.840.1.113894.1.8.31") (version 3.0; acl "Anonymous control access"; allow(read) userdn="ldap:///anyone";)'

str2='ds-cfg-global-aci: (targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10.2 || 1.3.6.1.4.1.42.2.27.8.5.1 || 2.16.840.1.113730.3.4.16 || 2.16.840.1.113894.1.8.31 || 1.2.840.113556.1.4.319 ") (version 3.0; acl "Anonymous control access"; allow(read) userdn="ldap:///anyone";)'

I want to replace 'str1' with 'str2' in a script using shell scripting.Can someone please help me out? I'm trying with sed, but I'm unable to do it.

I have tried using the following command :

sed -i 's_ ( ds-cfg-global-aci: \(targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10.2 || 1.3.6.1.4.1.42.2.27.8.5.1 || 2.16.840.1.113730.3.4.16 || )2.16.840.1.113894.1.8.31( "\) \(version 3.0 ; acl "Anonymous control access" ; allow\(read\) userdn="ldap:///anyone" ; \) ) _\12.16.840.1.113894.1.8.31 || 1.2.840.113556.1.4.319\2_' 2.xml

It doesn't throw any errors,but it doesn't yield the expected output.The filecontents of '2.xml' is just the pattern.

  • all that's changing is adding || 1.2.840.113556.1.4.319 ")? sed 's/2.16.840.1.113894.1.8.31/& || 1.2.840.113556.1.4.319")/' – mtklr Sep 7 '15 at 14:16
  • Following command : sed -i 's/2.16.840.1.113894.1.8.31/& || 1.2.840.113556.1.4.319/' worked for me. – Sandeep Suthari Sep 8 '15 at 6:22
1

I don't really understand what you're trying to do with that sed command,. You seem to be trying to capture some groups but then don't use them. Anyway, what you want is something like:

sed -i "s#$str1#$str2#" file

Or, if you don't have the strings saved as variables:

sed -i 's#ds-cfg-global-aci: (targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10.2 || 1.3.6.1.4.1.42.2.27.8.5.1 || 2.16.840.1.113730.3.4.16 || 2.16.840.1.113894.1.8.31") (version 3.0; acl "Anonymous control access"; allow(read) userdn="ldap:///anyone";)#ds-cfg-global-aci: (targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10.2 || 1.3.6.1.4.1.42.2.27.8.5.1 || 2.16.840.1.113730.3.4.16 || 2.16.840.1.113894.1.8.31 || 1.2.840.113556.1.4.319 ") (version 3.0; acl "Anonymous control access"; allow(read) userdn="ldap:///anyone";)#' file

Or, since the only difference seems to be that str1 doesn't contain || 1.2.840.113556.1.4.319, just add that:

sed -i 's/")/ || 1.2.840.113556.1.4.319 ")/' file

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.