2017-05-03 114 views
-2

我需要以下問題的幫助。我有一個包含以下數據的文件。懷疑組數據

21997|||70049,,20170428154818,20170527235959||| 
21997|||70070,,20170428154739,20170527235959||| 

21998|||70049,,20170428154818,20170527235959||| 
21998|||70070,,20170428154739,20170527235959||| 
21998|||70071,,20170428154739,20170527235959||| 

我需要統一文件如下。

21997|||70049,,20170502172844,20170531235959; 70070,,20170502172844,20170531235959||| 

21998|||70049,,20170502172844,20170531235959; 70070,,20170502172844,20170531235959; 70071,,20170502172844||| 

有人可以幫我嗎?

+3

嗨,歡迎來到Stack Overflow!如果您展示您嘗試過的內容,而不是爲您編寫所有代碼,那麼我們更喜歡它。你有什麼嘗試? [你可能想看看用SQLite做這個](http://stackoverflow.com/a/42563133/14660)。 – Schwern

+1

文件有多大?塊是否總是按第一列中的值分組,並用'\ n \ n'分隔? –

回答

-1
my $unified_output; 
my %out; 

open(FILE, "./raw-file.txt") or die $!; 
    my @file = <FILE>; 
close FILE; 

for (@file) {  
    next if $_ =~ /$^/;   
    my @line = split(/\|\|\|/, $_) if $_;  
    $out{"$line[0]"} .= qq~$line[1]; ~ if $_ and $_ =~ /^$line[0]/;  
} 

for (keys %out) { 
    $out{$_} =~ s!\; $!!; 
    $unified_output .= qq~$_|||$out{$_}|||\n~ if $_ and $out{$_}; 
} 
+0

我的朋友,我做了一個測試,但沒有奏效。 –

+1

親愛的朋友, 對不起,它工作。非常感謝你。 –

+0

唯一的問題是它應該爲每條記錄生成一行,但是爲其中一條記錄生成了兩行。見下: 21998 ||| 70049,,20170428154818,20170527235959; 70070,20170428154739,20170527235959; 70071,20170428154739,20170527235959 ||| 21998 ||| 70049,,20170428154818,20170527235959; 70070,20170428154739,20170527235959; 70071,20170428154739,20170527235959 ||| 21998 ||| 70049,,20170428154818,20170527235959; 70070,20170428154739,20170527235959; 70071,20170428154739,20170527235959 ||| 21997 ||| 70049,,20170428154818,20170527235959; 70070,20170428154739,20170527235959 ||| –