如何在每次sed比赛后添加“,”?

问题描述 投票:0回答:3

我有这个代码:

cat response_error.xml | sed  -ne  's#\s*<[^>]*>\s*##gp'  >> response_error.csv

但是来自xml的所有sed匹配都是绑定的,例如:

084521AntonioCallas 

我想得到这个效果

084521,Antonio,Callas, 

可能吗?

我必须编写一个从前一天收集XML文档的脚本,只从中提取没有<...>的数据,并以这种方式将此信息保存到csv文件:084521,Antonio,Callas - 用逗号分隔的信息。 XML看起来像这样:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GenerarInformeResponse xmlns="http://experian.servicios.CAIS">
<GenerarInformeResult>
<InformeResumen xmlns="http://experian.servicios.CAIS.V2">
<IdSuscriptor>084521</IdSuscriptor>
<ReferenciaConsulta>Antonio Callas 00000000</ReferenciaConsulta>
<Error>
<Codigo>0000</Codigo>
<Descripcion>OK</Descripcion>
</Error>
<Documento>
<TipoDocumento>
<Codigo>01</Codigo>
<Descripcion>NIF</Descripcion>
</TipoDocumento>
<NumeroDocumento>000000000</NumeroDocumento>
<PaisDocumento>
<Codigo>000</Codigo>
<Descripcion>ESPAÑA</Descripcion>
</PaisDocumento>
</Documento>
<Resumen>
<Nombre>
<Nombre1>XXX</Nombre1>
<Nombre2>XXX</Nombre2>
<ApellidosRazonSocial>XXX</ApellidosRazonSocial>
</Nombre>
<Direccion>
<Direccion>XXX</Direccion>
<NombreLocalidad>XXX</NombreLocalidad>
<CodigoLocalidad/>
<Provincia>
<Codigo>39</Codigo>
<Descripcion>XXX</Descripcion>
</Provincia>
<CodigoPostal>39012</CodigoPostal>
</Direccion>
<NumeroTotalOperacionesImpagadas>1</NumeroTotalOperacionesImpagadas>
<NumeroTotalCuotasImpagadas>0</NumeroTotalCuotasImpagadas>
<PeorSituacionPago>
<Codigo>6</Codigo>
<Descripcion>XXX</Descripcion>
</PeorSituacionPago>
<PeorSituacionPagoHistorica>
<Codigo>6</Codigo>
<Descripcion>XXX</Descripcion>
</PeorSituacionPagoHistorica>
<ImporteTotalImpagado>88.92</ImporteTotalImpagado>
<MaximoImporteImpagado>88.92</MaximoImporteImpagado>
<FechaMaximoImporteImpagado>
<DD>27</DD>
<MM>03</MM>
<AAAA>2019</AAAA>
</FechaMaximoImporteImpagado>
<FechaPeorSituaiconPagoHistorica>
<DD>27</DD>
<MM>03</MM>
<AAAA>2019</AAAA>
</FechaPeorSituaiconPagoHistorica>
<FechaAltaOperacionMasAntigua>
<DD>16</DD>
<MM>12</MM>
<AAAA>2015</AAAA>
</FechaAltaOperacionMasAntigua>
<FechaUltimaActualizacion>
<DD>27</DD>
<MM>03</MM>
<AAAA>2019</AAAA>
</FechaUltimaActualizacion>
</Resumen>
</InformeResumen>
</GenerarInformeResult>
</GenerarInformeResponse>
</s:Body>
</s:Envelope>   
bash sed text-processing
3个回答
0
投票

您可以使用以下命令提取IdSuscriptor

xmllint --xpath '//*[local-name()="IdSuscriptor"]/text()' response_error.xml

ReferenciaConsulta使用以下命令:

xmllint --xpath '//*[local-name()="ReferenciaConsulta"]/text()' response_error.xml

为了生成所需的IdSubscriptor,FirstName,LastName,我将使用以下脚本:

id_suscriptor=$(xmllint --xpath '//*[local-name()="IdSuscriptor"]/text()' response_error.xml)
referencia_consulta=$(xmllint --xpath '//*[local-name()="IdSuscriptor"]/text()' response_error.xml)
first_name=$(echo "$referencia_consulta" | cut -f1)
last_name=$(echo "$referencia_consulta" | cut -f2)
echo "$id_suscriptor,$first_name,$last_name"

请注意,这假设ReferenciaConsulta字段将始终包含以第一个名称开头的字符串,并以空格分隔。


0
投票

如果要解析XML,请使用Saxon之类的专用XML解析器。

如果你想用一些有趣的无关尖括号解析一个奇怪的文本文件,试试这个:

#! /bin/sed -nf

s/^<IdSuscriptor>\([0-9]\+\)<\/IdSuscriptor>/\1,/
t match1
b next

: match1
h
b

: next
s/^<ReferenciaConsulta>\([^ ]\+\) \([^ ]\+\) [0-9]\+<\/ReferenciaConsulta>/\1,\2,/
t match2
b

: match2
H
g
s/\n//
p

Explanation

qazxsw poi跳到qazxsw poi,如果前面的qazxsw poi命令做了替换。否则t跳到match1

如果匹配,s将匹配的字符串复制到保留空间,b停止当前行的处理。

第二个next命令以不同的方式工作,如果没有匹配,h继续下一行。

在第二次匹配的情况下,b将模式空间附加到保留空间,s将保持空间复制到模式空间,b删除两个匹配之间的换行符,H打印结果。

Conclusion

如果您不知道如何使用g,请不要尝试。尝试学习像Perl,JavaScript或Python这样的真实编程语言。 s是过去时代的遗物。


0
投票

如果你的数据在'd'文件中,请尝试gnu sed:

p
© www.soinside.com 2019 - 2024. All rights reserved.