2016-06-21 5 views
-2

다음과 같이 XML이 있습니다. <com.eds.travel.fares.ping.response></com.eds.travel.fares.ping.response> 사이의 텍스트를 추출하고 싶습니다. XML은 com.eds.travel.fares.ping.response으로 시작하고 com.eds.travel.fares.ping.response으로 끝납니다.XML에서 여러 줄의 텍스트를 추출합니다.

<?xml version="1.0" encoding="UTF-8"?> 
<!--This is a Ping Response--> 
<com.eds.travel.fares.ping.response xmlns="http://schemas.eds.com/transportation/message/ping/response" targetNamespace="http://schemas.eds.com/transportation/message/ping/response" EchoToken="00c0d1a" TimeStamp="2016-06-21T00:01:48.191" Target="Test" Version="1.07" SequenceNmbr="1466467309030" PrimaryLangID="en" RequestorCompanyCode="1y" RequestorNetworkID="as" SetLocation="zrh"> 
<Headers Trailers="n"> 
    <Result xmlns="http://schemas.eds.com/transportation/message/fares/common" status="success" /> 
</Headers> 
<DataArea> 
    <Pong Message="pong" ServerHostName="usclsefam922.clt.travel.eds.com" ServerPortNumber="8024" ServerMessageCount="1" RegionName="preprod" SystemName="preprods3.1" SystemDate="20160621" SystemTime="148" CodeVersion="$Name: build-2016-06-17-1338 $" /> 
</DataArea> 
<Trailers /> 
</com.eds.travel.fares.ping.response> 

나는 아래의 명령하지만 운 시도 :

cat file.txt | egrep "<com.eds.travel.fares.ping.response>.*</com.eds.travel.fares.ping.response>" 

은 알려 주시기 바랍니다.

+3

? 이것은 파서의 일처럼 들리므로 ... XML을 구문 분석하여 의미 상 동일한 XML을 얻으려고합니다. 정말 조금 이상하고 XY 문제처럼 들립니다. – Sobrique

+1

([고양이의 쓸모없는 사용.] (https://en.wikipedia.org/wiki/Cat_ (Unix) #Useless_use_of_cat)) – Biffen

답변

0

나는 egrep을 여러 줄 일치하지 않을 수 있습니다 보인다 시도했다, 사용 pcregrep -M 대신

pcregrep -M 'com.eds.travel.fares.ping.response((.|\n)*)com.eds.travel.fares.ping.response' 

규칙

0

나를 위해 XML 중 하나를 속임수를 썼는지 수에서. 정규식을 사용하지 마십시오. XML은 컨텍스트 언어이며 정규 표현식은이를 수행 할 수 없습니다. 당신은 부서지기 쉬운 해킹을 할 것이고, 언젠가는 XML이 완벽하게 유효 할 때 신비하게 깨뜨릴 것입니다.

대신 파서를 사용하십시오. Perl에는 몇 가지 옵션이 있습니다. 좋은 시작점 (XML::LibXML도 뛰어나지 만 더 가파른 학습 곡선)이라는 이유로 XML::Twig을 좋아합니다.

그리고 이것에 대한

, 당신이 필요로하는 모든입니다

#!usr/bin/perl 
use strict; 
use warnings; 

use XML::Twig; 

my $twig = XML::Twig -> new (comments => 'drop')->parse (\*DATA); 
$twig -> set_pretty_print('indented_a'); 
$twig -> get_xpath('//com.eds.travel.fares.ping.response',0 ) -> print; 


__DATA__ 
<?xml version="1.0" encoding="UTF-8"?> 
<!--This is a Ping Response--> 
<com.eds.travel.fares.ping.response xmlns="http://schemas.eds.com/transportation/message/ping/response" targetNamespace="http://schemas.eds.com/transportation/message/ping/response" EchoToken="00c0d1a" TimeStamp="2016-06-21T00:01:48.191" Target="Test" Version="1.07" SequenceNmbr="1466467309030" PrimaryLangID="en" RequestorCompanyCode="1y" RequestorNetworkID="as" SetLocation="zrh"> 
<Headers Trailers="n"> 
    <Result xmlns="http://schemas.eds.com/transportation/message/fares/common" status="success" /> 
</Headers> 
<DataArea> 
    <Pong Message="pong" ServerHostName="usclsefam922.clt.travel.eds.com" ServerPortNumber="8024" ServerMessageCount="1" RegionName="preprod" SystemName="preprods3.1" SystemDate="20160621" SystemTime="148" CodeVersion="$Name: build-2016-06-17-1338 $" /> 
</DataArea> 
<Trailers /> 
</com.eds.travel.fares.ping.response> 

이 출력 - 요청한 : 기본적 XML의 모든 덜 헤더와 의견이다

<com.eds.travel.fares.ping.response 
    EchoToken="00c0d1a" 
    PrimaryLangID="en" 
    RequestorCompanyCode="1y" 
    RequestorNetworkID="as" 
    SequenceNmbr="1466467309030" 
    SetLocation="zrh" 
    Target="Test" 
    TimeStamp="2016-06-21T00:01:48.191" 
    Version="1.07" 
    targetNamespace="http://schemas.eds.com/transportation/message/ping/response" 
    xmlns="http://schemas.eds.com/transportation/message/ping/response"> 
    <Headers Trailers="n"> 
    <Result 
     status="success" 
     xmlns="http://schemas.eds.com/transportation/message/fares/common" 
    /> 
    </Headers> 
    <DataArea> 
    <Pong 
     CodeVersion="$Name: build-2016-06-17-1338 $" 
     Message="pong" 
     RegionName="preprod" 
     ServerHostName="usclsefam922.clt.travel.eds.com" 
     ServerMessageCount="1" 
     ServerPortNumber="8024" 
     SystemDate="20160621" 
     SystemName="preprods3.1" 
     SystemTime="148" 
    /> 
    </DataArea> 
    <Trailers/> 
</com.eds.travel.fares.ping.response> 

합니다. 기술적으로 당신이 요구했던 것, 그러나 사소한 것. 다시 포맷팅하는 XML은 이며, 유효 숫자는 모두입니다. 그래서 regex 기반 솔루션이 중단됩니다.

어떻게 대신 약 : 소스 데이터, 프린트와

#!usr/bin/perl 
use strict; 
use warnings; 

use XML::Twig; 

my $twig = XML::Twig -> parsefile ('file.txt'); 

foreach my $pong ($twig -> get_xpath('//Pong')) { 
    foreach my $key(keys %{$pong -> atts}) { 
     print "$key => ", $pong -> att($key),"\n"; 
    } 
} 

어느 : 정확히 추출하려고하는 어떤

CodeVersion => $Name: build-2016-06-17-1338 $ 
RegionName => preprod 
SystemTime => 148 
ServerHostName => usclsefam922.clt.travel.eds.com 
SystemDate => 20160621 
SystemName => preprods3.1 
ServerMessageCount => 1 
ServerPortNumber => 8024 
Message => pong 
관련 문제