2017-04-22 9 views
0

つのジョブがサーバー上で実行される値の交換後にレコード数をカウントし、それは以下のようにファイルを作成します。現在、重複が

1000727888004 
522101 John Smith 
522101 John Smith 
522188 Shelly King 
522188 Shelly King 
1000727888002 
522990 John Doe 
522990 John Doe 
9000006000000 

、我々は、コードを修正する過程にあるが、それは月がかかります。その間に、以下のような重複レコードを削除するコマンドを使用しています。私は上記のコマンドを実行した後

perl -ne 'print unless $dup{$_}++;' old_file.txt > new_file.txt 

、それが重複したエントリを削除しますが、数が同じ以下のように残っている:1で始まる行に対する

1000727888004 
522101 John Smith 
522188 Shelly King 
1000727888002 
522990 John Doe 
9000006000000 

最後の数は総数である(その4はされている必要があります1行目は2、4行目は1でなければならず、6は9で始まる最後の行では3であったはずです)。以下のようになります。

1000727888002 
522101 John Smith 
522188 Shelly King 
1000727888001 
522990 John Doe 
9000003000000 

これを修正するロジックはありませんでした。私はここで助けが必要です。別のコマンドを実行したり、perlコマンドで何かを追加してカウントを修正したりすることはできますか?はい、メモ帳でファイルを開くことができます+ +と手動で番号を修正するが、私はそれを自動化しようとしています。

ありがとうございます!

+0

最後のレコードは、9で始まることは何ですか? –

+0

は、合計カウントを持つファイルのトレーラです。最初の9が常に存在し、次の6つの数字がカウントです.1つの数字の場合は、5つのゼロが左に埋められます。最後の6つの数字は常に0です – Amir

答えて

0

awk。これは、カウントレコード間の "ブロック"内の二重引用符を処理します。それはファイル全体の重複を考慮しません。これが間違っている場合は、私に知らせてください。

$ awk ' 
NF==1 {   # for the cout record 
    if(c!="") # this fixes leading empty row 
     print c # print count 
    for(i in a) # all deduped data records 
     print i # print them 
    delete a  # empty hash 
    c=$0   # store count (well, you could use just the first count record) 
    next   # for this record don't process further 
} 
{ 
    if($0 in a) # if current record is already in a 
     c--  # decrease count 
    else a[$0] # else hash it 
} 
END {   # last record handling 
    print c  # print the last record 
    for(i in a) # just in case last record would be missing 
     print i # this and above could be removes 
}' file 

出力:

1000727888002 
522101 John Smith 
522188 Shelly King 
1000727888001 
522990 John Doe 
9000006000000 

dupesは、ファイル全体で除去し、最後のレコードも数だったしている場合:

awk ' 
NF==1 { 
    if(NR==1) 
     c=$0 
    print c 
} 
NF>1 { 
    if($0 in a) 
     c-- 
    else { 
     a[$0] 
     print 
    } 
}' file 
1000727888004 
522101 John Smith 
522188 Shelly King 
1000727888002 
522990 John Doe 
1000727888001