Edite el archivo xml usando el script/comando de shell

Edite el archivo xml usando el script/comando de shell

Necesito hacer esto usando un script o comando Unix. Hay un archivo xml en /home/user/app/xmlfiles como

<book>
   <fiction type='a'>
      <author type=''></author>
   </fiction>
   <fiction type='b'>
      <author type=''></author>
   </fiction>
   <Romance>
       <author type=''></author>
   </Romance>
</book>

Quiero editar el tipo de autor en ficción como local.

   <fiction>
      <author type='Local'></author>
   </fiction>

Necesito cambiar el tipo de autor que está enetiqueta de ficción con atributo bsolo. Por favor ayúdenme con esto usando un script o comando de shell de Unix. Gracias !

Respuesta1

Si solo desea reemplazar <author type=''><\/author>con <author type='Local'><\/author>, puede usar ese sedcomando:

sed "/<fiction type='a'>/,/<\/fiction>/ s/<author type=''><\/author>/<author type='Local'><\/author>/g;" file

Pero, cuando se trata de xml, recomiendo un analizador/editor xml comoxmlestrella:

$ xmlstarlet ed -u /book/*/author[@type]/@type -v "Local"  file
<?xml version="1.0"?>
<book>
  <fiction>
    <author type="Local"/>
  </fiction>
  <Romance>
    <author type="Local"/>
  </Romance>
</book>

Utilice la -Lbandera para editar el archivo en línea, en lugar de imprimir los cambios.

Respuesta2

xmlstarlet edit --update "/book/fiction[@type='b']/author/@type" --value "Local" book.xml

Respuesta3

Podríamos usar un documento xsl doThis.xsly procesarlo en un source.xmlarchivo .xsltprocnewFile.xml

El xsl se basa en la respuesta a esto.pregunta.

Pon esto en un doThis.xslarchivo

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no"/> 

<!-- Copy the entire document    -->

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- Copy a specific element     -->

<xsl:template match="/book/fiction[@type='b']/author">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>

<!--    Do something with selected element  -->
            <xsl:attribute name="type">Local</xsl:attribute>

        </xsl:copy>
</xsl:template>

</xsl:stylesheet> 

Ahora producimos elnewFile.xml

$:   xsltproc -o ./newFile.xml ./doThis.xsl ./source.xml 

Este será elnewFile.xml

<?xml version="1.0" encoding="UTF-8"?>
<book>
   <fiction type="a">
      <author type=""/>
   </fiction>
   <fiction type="b">
      <author type="Local"/>
   </fiction>
   <Romance>
       <author type=""/>
   </Romance>
</book>

La expresión utilizada para encontrar ficción tipo b es XPath.

Respuesta4

Es bastante fácil con sed. El siguiente script cambiará el contenido del archivo a.xmly colocará el original a.bakcomo copia de seguridad.

Lo que hace es buscar la cadena en cada archivo <author type=''>y la reemplaza con <author type='Local'>. El /gmodificador significa que intentará realizar más de 1 reemplazo en cada línea si es posible (no es necesario para su archivo de ejemplo).

sed -i.bak "s/<author type=''>/<author type='Local'>/g" a.xml

información relacionada