当前位置: 动力学知识库 > 问答 > 编程问答 >

bash - unterminated address regex on sed command

问题描述:

Im trying to write an XML tag in to a file. and i want that to be above another tag

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

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

Expected Result:

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

The Log when i execute script.sh

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

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

Can some one help me to identify the issue?

网友答案:

Drop the single quotes from your sed call:

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

Your expected result includes the afp:AttributeFilterPolicyGroup element which is absent rom the attribute_filter.xml that you specify, i take it, that the actual file contains said element.

Reconsider whether you really wish to transform a serialized tree structure with a line-oriented tool like sed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: