我试图替换以下行
<application-bnd>
<security-role name="appAdmin">
<user name="appadmin"/>
</security-role>
<security-role name="RestAdmin">
<user name="appadmin"/>
</security-role>
<security-role name="ConsoleAdmin">
<user name="appadmin"/>
</security-role>
<security-role name="ConsoleUser">
<special-subject type="ALL_AUTHENTICATED_USERS" />
</security-role>
</application-bnd>
到
<application-bnd>
<security-role name="ALL_AUTHENTICATED_CONTAINER">
<special-subject type="ALL_AUTHENTICATED_USERS"/>
</security-role>
</application-bnd>
答案 0 :(得分:0)
你可以尝试这个解决方案,但是我从你的文件中删除了新行,所以如果你不想删除换行符,或者你在application-bnd之后有安全角色,那么这将不起作用
cat text.xml | tr -d '\n' | sed 's%<security-role.*<security-role%<security-role name="ALL_AUTHENTICATED_CONTAINER"><security-role%'
答案 1 :(得分:0)
这里有一个awk而不是sed的解决方案,可以完成你的工作
awk '
BEGIN {
enable=0;
lineToRemove == 0;
}
/<application-bnd/ {
replacementStart=1;
}
/<\/application-bnd/ {
replacementStart=0; enable=0;
}
/<security-role/ {
if (replacementStart==1) enable=1;
}
/<\/security-role/ {
if (replacementStart==1) enable=0; lineToRemove = 1; }
/ALL_AUTHENTICATED_USERS/ {
print "<security-role name=\"ALL_AUTHENTICATED_CONTAINER\">";
print "<special-subject type=\"ALL_AUTHENTICATED_USERS\"/>";
print "</security-role>";
}
/.*/ {
if (enable==0 && lineToRemove == 0) print $0;
lineToRemove = 0;
}' text.xml