简体   繁体   English

Bash在变量上使用Sed

[英]Bash Using Sed on a Variable

I have an xml file which will always have the following block inside of it: 我有一个xml文件,其中始终包含以下块:

<Name NameType="Also Known As">
  <NameValue>
    <FirstName>name1</FirstName>
    <Surname>sur1</Surname>
  </NameValue>
  <NameValue>
    <FirstName>name2</FirstName>
    <Surname>sur2</Surname>
  </NameValue>
</Name>

There may be just one "NameValue" node inside this block or there may be many. 此块内可能只有一个“ NameValue”节点,也可能有很多。 My issue is that I only ever want to keep the first one and discard the rest. 我的问题是,我只想保留第一个,其余的就丢弃。 The example above would hence read: 因此,以上示例将显示为:

<Name NameType="Also Known As">
  <NameValue>
    <FirstName>name1</FirstName>
    <Surname>sur1</Surname>
  </NameValue>
</Name>

I'd like to use sed for this purpose. 我想为此使用sed。 I have written the above block to a variable and tried to manipulate that as follows: 我已将上面的代码块写入变量,并尝试如下操作:

var=$(sed -n '/<Name NameType="Also Known As">*/,/<\/Name>/p' $file)
sed -i 's/<\/NameValue>.*<\/Name>/<\/NameValue><\/Name>/g' "$var"

I get the following in return: 我得到以下回报:

sed: can't read <Name NameType="Also Known As">...

I thought the above would have replaced everything between the first closing NameValue tag and the closing Name tag with nothing. 我以为上面的内容可以完全替换掉第一个封闭的NameValue标签和封闭的Name标签之间的所有内容。 I used the -i option as I want to save the changes to this file. 我使用-i选项是因为要将更改保存到此文件中。 Perhaps it's syntax related, or maybe my understanding of using the sed command on a variable is way off. 也许与语法有关,或者我对在变量上使用sed命令的理解还很遥远。 A point to note is that these tags occur inside other nodes in the file and so doing one blanket update using sed would change blocks which i don't want to change. 需要注意的一点是,这些标记出现在文件的其他节点内,因此使用sed进行一次全局更新将更改我不想更改的块。 This is the reason I passed this block to a variable. 这就是我将此块传递给变量的原因。 Any pointer's in the right direction would be much appreciated. 任何朝着正确方向的指针将不胜感激。

As the previous comments point out, sed expects one or several input files (which may be something like stdin, of course). 正如前面的评论所指出的那样, sed需要一个或几个输入文件(当然,它可能像stdin一样)。 In your example code, you read the content of a file into a variable in a roundabout way, and then use that variable as the name of a file. 在你的示例代码,可以读取文件的内容转换成以一种迂回的方式变量,然后使用该变量作为文件 That is bound to fail. 那注定会失败的。 While I'm sure you could achieve your objectives using sed , I do not advise it. 虽然我确定您可以使用sed实现您的目标,但我不建议这样做。 Branching in sed is horrible. sed分支非常可怕。 Use a more powerful tool, preferably xsltproc or something else that is designed to work with XML. 使用功能更强大的工具,最好是xsltproc或其他设计用于XML的工具。

You could use a stylesheet similar to this: 您可以使用类似于以下的样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Name">
    <Name>
    <xsl:copy-of select="@*" />
    <xsl:copy-of select="*[1]" />
    </Name>
  </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM