sed命令的未终止地址正则表达式

时间:2015-05-29 09:30:01

标签: regex bash shell sed

我正在尝试将XML标记写入文件。我希望它高于另一个标签

Script.sh

sudo chmod 777 attribute-filter.xml
 configure_attribute_filter_tag1='<afp:AttributeFilterPolicy id="uid">'
 configure_attribute_filter_tag12='</afp:AttributeFilterPolicy>'
  configure_attribute_filter_var='<afp:AttributeFilterPolicy id="releaseTransientIdToAnyone">'
      sed -i '/'$configure_attribute_filter_var'/i \'"$configure_attribute_filter_tag1"'' attribute-filter.xml
     sed -i '/'$configure_attribute_filter_var'/i \'"$configure_attribute_filter_tag12"'' attribute-filter.xml

属性filter.xml

<afp:AttributeFilterPolicy id="releaseTransientIdToAnyone">
    <afp:PolicyRequirementRule xsi:type="basic:ANY"/>

    <afp:AttributeRule attributeID="transientId">
        <afp:PermitValueRule xsi:type="basic:ANY"/>
    </afp:AttributeRule>

</afp:AttributeFilterPolicy>

预期结果:

    <afp:AttributeFilterPolicyGroup id="ShibbolethFilterPolicy"
                                    xmlns:afp="urn:mace:shibboleth:2.0:afp" xmlns:basic="urn:mace:shibboleth:2.0:afp:mf:basic" 
                                    xmlns:saml="urn:mace:shibboleth:2.0:afp:mf:saml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                                    xsi:schemaLocation="urn:mace:shibboleth:2.0:afp classpath:/schema/shibboleth-2.0-afp.xsd
                                                        urn:mace:shibboleth:2.0:afp:mf:basic classpath:/schema/shibboleth-2.0-afp-mf-basic.xsd
                                                        urn:mace:shibboleth:2.0:afp:mf:saml classpath:/schema/shibboleth-2.0-afp-mf-saml.xsd">
   <afp:AttributeFilterPolicy id="uid">
    </afp:AttributeFilterPolicy>

        <afp:AttributeFilterPolicy id="releaseTransientIdToAnyone">
            <afp:PolicyRequirementRule xsi:type="basic:ANY"/>

            <afp:AttributeRule attributeID="transientId">
                <afp:PermitValueRule xsi:type="basic:ANY"/>
            </afp:AttributeRule>

        </afp:AttributeFilterPolicy>
    </afp:AttributeFilterPolicyGroup>

执行script.sh时的日志

sed: -e expression #1, char 27: unterminated address regex
sed: -e expression #1, char 27: unterminated address regex

有人可以帮助我找出问题吗?

1 个答案:

答案 0 :(得分:2)

从您的sed电话中删除单引号:

sed -i "/$configure_attribute_filter_var/i $configure_attribute_filter_tag1" attribute-filter.xml

您的预期结果包括您指定的afp:AttributeFilterPolicyGroup缺少的attribute_filter.xml元素,我认为实际文件包含所述元素。

重新考虑是否真的希望使用像sed这样的面向行的工具转换序列化树结构。