I want remove below part from the file which is added below.
Not able to remove
credentials.admin.adapters
this property from fileIts deleting the others property in the same line.
How to add content between
< >
this character in same line.<!--<UndefineProperty name="credentials.admin.adapters"/>--> <!--<AddToProperty name="credentials.admin.adapters" value="com.Ldap^cn=Manager^encrypted.com.Ldap.cn=admin"/>--> <Property name="credentials.admin.adapters" overridable="true" targetFile="Credentials.txt" value="com.Ldap^id=proxy"/> <!--<UndefineProperty name="credentials.admin.adapters"/> <AddToProperty name="credentials.admin.adapters" value="com.Ldap^cn=admin^encrypted.com.Ldap.cn=admin"/> -->
newconf.txt
<!--<UndefineProperty name="credentials.admin.adapters"/>-->
<!--<AddToProperty name="credentials.admin.adapters"
value="com.Ldap^cn=Manager^encrypted.com.Ldap.cn=admin"/>-->
<Property name="credentials.admin.adapters" overridable="true"
targetFile="Credentials.txt"
value="com.Ldap^id=proxy"/>
<Property name="pom.dbUser" overridable="true" targetFile="db.properties"
value="NM"/>
<Property name="pom.jdbc.host" overridable="true" targetFile="db.properties"
value="0038.rds.amazonaws.com"/>
<Property name="pom.dbPassword" overridable="true" targetFile="db.properties"
value="encrypted.pom.dbPassword"/>
<Property name="pom.serviceName" overridable="true" targetFile="db.properties"
value="rds.amazonaws.com:10:0038"/>
<Property name="pom.jdbc.service" overridable="true" targetFile="db.properties"
value="0038"/>
<Property name="pom.jdbc.port" overridable="true" targetFile="db.properties"
value="10"/>
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
<ConfigurationRef xlink:href="ser.xconf"/>
<!--<UndefineProperty name="credentials.admin.adapters"/>
<AddToProperty name="credentials.admin.adapters"
value="com.Ldap^cn=admin^encrypted.com.Ldap.cn=admin"/> --><Property
name="com.distproc.credential" overridable="true"
targetFile="distproc.properties"
value=""/>
</Configuration>
I tried below:
sed -i '/value=/N; s/\n\s*/ /g' newconf.txt
sed -i "/credentials.admin.adapters/d" newconf.txt
But above code deleting the others property also.
I want below output:
<Property name="pom.dbUser" overridable="true" targetFile="db.properties" value="NM"/>
<Property name="pom.jdbc.host" overridable="true" targetFile="db.properties" value="0038.rds.amazonaws.com"/>
<Property name="pom.dbPassword" overridable="true" targetFile="db.properties" value="encrypted.pom.dbPassword"/>
<Property name="pom.serviceName" overridable="true" targetFile="db.properties" value="rds.amazonaws.com:10:0038"/>
<Property name="pom.jdbc.service" overridable="true" targetFile="db.properties" value="0038"/>
<Property name="pom.jdbc.port" overridable="true" targetFile="db.properties" value="10"/>
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
<ConfigurationRef xlink:href="ser.xconf"/>
<Property name="com.distproc.credential" overridable="true" targetFile="distproc.properties" value=""/>
</Configuration>
Delete below line:
<!--<UndefineProperty name="credentials.admin.adapters"/>-->
<!--<AddToProperty name="credentials.admin.adapters" value="com.Ldap^cn=Manager^encrypted.com.Ldap.cn=admin"/>-->
<Property name="credentials.admin.adapters" overridable="true" targetFile="Credentials.txt" value="com.Ldap^id=proxy"/>
<!--<UndefineProperty name="credentials.admin.adapters"/>
<AddToProperty name="credentials.admin.adapters" value="com.Ldap^cn=admin^encrypted.com.Ldap.cn=admin"/> -->
CodePudding user response:
Your question isn't clear but is this what you're trying to do (using any awk in any shell on every Unix box):
$ cat tst.awk
{ rec = (NR>1 ? rec ORS : "") $0 }
END {
tgt = "name=\"credentials[.]admin[.]adapters\""
$0 = rec
gsub(/@/,"@X")
gsub(/{/,"@A")
gsub(/}/,"@B")
gsub(/<!--/,"{")
gsub(/-->/,"}")
while ( match($0,"[{][^}]*" tgt "[^}]*}") ) {
$0 = substr($0,1,RSTART-1) substr($0,RSTART RLENGTH)
}
while ( match($0,"[<][^>]*" tgt "[^>]*}") ) {
$0 = substr($0,1,RSTART-1) substr($0,RSTART RLENGTH)
}
gsub(/}/,"-->")
gsub(/{/,"<!--")
gsub(/@B/,"}")
gsub(/@A/,"{")
gsub(/@X/,"@")
print
}
$ awk -f tst.awk newconf.txt
<Property name="credentials.admin.adapters" overridable="true"
targetFile="Credentials.txt"
value="com.Ldap^id=proxy"/>
<Property name="pom.dbUser" overridable="true" targetFile="db.properties"
value="NM"/>
<Property name="pom.jdbc.host" overridable="true" targetFile="db.properties"
value="0038.rds.amazonaws.com"/>
<Property name="pom.dbPassword" overridable="true" targetFile="db.properties"
value="encrypted.pom.dbPassword"/>
<Property name="pom.serviceName" overridable="true" targetFile="db.properties"
value="rds.amazonaws.com:10:0038"/>
<Property name="pom.jdbc.service" overridable="true" targetFile="db.properties"
value="0038"/>
<Property name="pom.jdbc.port" overridable="true" targetFile="db.properties"
value="10"/>
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
<ConfigurationRef xlink:href="ser.xconf"/>
<Property
name="com.distproc.credential" overridable="true"
targetFile="distproc.properties"
value=""/>
</Configuration>
The blank lines at the start are because when you remove the strings you want removed the newlines around them are left behind.
CodePudding user response:
Using sed
$ sed -z ':a;s/\([^\n]*\)\n \ /\1 /;ta;s/>\(<[^>]*>\) /\n\1\n/' input_file | sed '/credentials\.admin\.adapters/d'
<Property name="pom.dbUser" overridable="true" targetFile="db.properties" value="NM"/>
<Property name="pom.jdbc.host" overridable="true" targetFile="db.properties" value="0038.rds.amazonaws.com"/>
<Property name="pom.dbPassword" overridable="true" targetFile="db.properties" value="encrypted.pom.dbPassword"/>
<Property name="pom.serviceName" overridable="true" targetFile="db.properties" value="rds.amazonaws.com:10:0038"/>
<Property name="pom.jdbc.service" overridable="true" targetFile="db.properties" value="0038"/>
<Property name="pom.jdbc.port" overridable="true" targetFile="db.properties" value="10"/>
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
<ConfigurationRef xlink:href="ser.xconf"/>
<Property name="com.distproc.credential" overridable="true" targetFile="distproc.properties" value=""/>
</Configuration>
CodePudding user response:
one big FS
and 2 calls to gsub()
is all you need :
{m,g}awk '
gsub("[ \t]*\n [ \t] "," ",$!(NF=NF)) gsub("[>][\t ]*\n] [<]",">\n<")^_' RS='^$' OFS=\
FS='([<]!--)?[<][^></=] =\"credentials[.]admin[.]adapters\"[^>]*[>]( *--[>])?\n*|\n $'
=
<Property name="pom.dbUser" overridable="true" targetFile="db.properties" value="NM"/>
<Property name="pom.jdbc.host" overridable="true" targetFile="db.properties" value="0038.rds.amazonaws.com"/>
<Property name="pom.dbPassword" overridable="true" targetFile="db.properties" value="encrypted.pom.dbPassword"/>
<Property name="pom.serviceName" overridable="true" targetFile="db.properties" value="rds.amazonaws.com:10:0038"/>
<Property name="pom.jdbc.service" overridable="true" targetFile="db.properties" value="0038"/>
<Property name="pom.jdbc.port" overridable="true" targetFile="db.properties" value="10"/>
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
<ConfigurationRef xlink:href="ser.xconf"/>
<Property name="com.distproc.credential" overridable="true" targetFile="distproc.properties" value=""/>
</Configuration>