如何删除两行之间的空格,使其成为一行,并使用shell脚本删除

w46czmvw  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(179)

我想从下面添加的文件中删除下面的部分。
1.无法从文件中删除此属性credentials.admin.adapters
1.删除同一行中的其他属性。
1.如何在同一行中的< >字符之间添加内容。

<!--<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"/> -->

新配置文件

<!--<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>

我试了试下面:

sed -i '/value=/N; s/\n\s*/ /g' newconf.txt
sed -i "/credentials.admin.adapters/d" newconf.txt

但是上面的代码也删除了其他人的属性。
我想要以下输出:

<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>

删除以下行:

<!--<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"/> -->
wgxvkvu9

wgxvkvu91#

使用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>

低于不稳定输出:

value="NM"/>e="pom.dbUser" overridable="true" targetFile="db.properties"
value="0038.rds.amazonaws.com"/>erridable="true" 
targetFile="db.properties"
value="encrypted.pom.dbPassword"/>rridable="true" 
targetFile="db.properties"
value="rds.amazonaws.com:10:0038"/>rridable="true" 
targetFile="db.properties"
value="0038"/>"pom.jdbc.service" overridable="true" 
 targetFile="db.properties"
value="10"/>e="pom.jdbc.port" overridable="true" 
targetFile="db.properties"
<ConfigurationRef xlink:href="utilities/configurator.xconf"/>
<ConfigurationRef xlink:href="owl.xconf"/>
 <ConfigurationRef xlink:href="ser.xconf"/>
dbf7pr2w

dbf7pr2w2#

一个大的FS和两个对gsub()的调用就足够了:

{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>

相关问题