簡體   English   中英

ACARS消息解析

[英]ACARS message parsing

我需要將ACARS消息解析為XML格式。

有簡單的消息:

RX_IDX: 13
ACARS mode: O, message label: 5V
ACARS ML description: VDL switch advisory
Aircraft reg: .EI-EUX, flight id: UN0323
Block id: 57,  msg. no: S91A
Message content:-

----------------------------------------------------------[05/05/2013 08:58]

RX_IDX: 14
ACARS mode: 2, message label: 1L
ACARS ML description: Off message
Aircraft reg: .D-AIRO, flight id: LH1490
Aircraft vendor: Airbus, short type: A321, full type: A321-131, cn: 0563
Carrier IATA: LH, ICAO: DLH, remarks: Lufthansa
Airlines: Lufthansa
Block id: 56,  msg. no: M03A
Message content:-
00002216743GO,X,55655
----------------------------------------------------------[05/05/2013 09:24]

每個消息均以RX_IDX開頭,並以日期結尾(例如[05/05/2013 09:24])。

我找到了perl腳本,但是逗號后不能識別屬性。

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

my @keys = ( 
    'RX_IDX',
    'ACARS mode',
    'message label',
    'ACARS ML description',
    'Aircraft reg',
    'flight id',
    'Aircraft vendor',
    'short type',
    'full type',
    'cn',
    'Carrier IATA',
    'ICAO',
    'remarks',
    'Airlines',
    'Block id',
    'msg. no',
    'Message content'
);

my( %keys, %tags );
$keys{$_} = 1 for @keys;
$tags{$_} = $_ . '' for @keys;
$tags{$_} =~ s/ /_/g for @keys;

my $file = 'data8.txt';
open( my $fh, '<', $file) or die("Can't open $file: $!");

my %record = map { $_, '' } @keys;
while( my $line = <$fh> ) {
    chomp($line);
    if( $line =~ m{ \A (.+?) : \s* (\S+) }x ) {
        $record{$1} = $2 if $keys{$1};
        if( $1 eq $keys[$#keys] ) {
            print "<Message>\n";
            print "<$tags{$_}>$record{$_}</$tags{$_}>\n" for @keys;
            print "</Message>\n";
            %record = map { $_, '' } @keys;
        }
    }
}

問候

問題是正則表達式的if條件僅對每行匹配一次。 嘗試匹配正則表達式,直到在while循環中失敗為止。 我在下一個循環中添加了\\G斷言,它將在上次離開時​​開始。 還作了一些更改以避免在行( \\A )開頭匹配,並在結尾添加了逗號的可能匹配,就像這樣(我只復制了代碼的相關部分):

while( my $line = <$fh> ) { 
    chomp($line);
    while ( $line =~ m{ \G \s* (.+?) \s* : \s* ([^,]+) \s* (?:,|$) }xg ) { 
        $record{$1} = $2 if $keys{$1};
        if( $1 eq $keys[$#keys] ) { 
            ...
        }   
    }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM