1

ログ ファイルをソートするための Perl コードを書くための助けを探しています。

私はコーディングと perl 全般に関して比較的初心者です。

可能な限りコア perl モジュールだけでコードを書く必要がありますが、それが不可能であることが判明した場合は、CPAN モジュールを受け入れます。ログ ファイルには、ログに記録されたメッセージのリストが含まれており、順番に並べ替える必要があります。十分に単純なはずですが、落とし穴がたくさんあり、データ構造の設計方法に問題が生じています。入力ファイル形式は CSV であり、出力はタイムスタンプ順のメッセージと同じである必要があり、最初のメッセージ部分が最初にグループ化された連結メッセージである必要があります。

落とし穴

  1. メッセージはタイムスタンプ順に並べる必要があります。
  2. メッセージが複数の行に分割されている場合、最後のフィールドに次のような内容が表示されます "(メッセージ参照 1 のパート 1/3)"。特定のメッセージ参照では、すべてのパーツを順番に並べる必要があります。したがって、パーツ 1 の後にパーツ 2 が続き、パーツ 3 が続きます。
  3. このフィールドの先頭にある 16 進数は、それが 8 ビット参照か 16 ビット参照かを示しており、同じ参照番号を持つ 8 ビット参照は、同じ番号を持つ 16 ビット参照と (重複として) 一致しません。だから私はこれを考慮に入れる必要があります。
  4. メッセージ パーツが欠落している可能性があるため、3 つのうち 1 と 2 のパーツしか取得できない場合があります。
  5. メッセージ参照番号が重複する可能性があるため、各メッセージ参照を from フィールドに関連付けて一意の ID を与える必要があります。
  6. (3) の一意の ID を使用しても、時間の経過とともに重複が発生する可能性があります (リセットされる前のメッセージ参照番号の数は非常に限られているため)。そのため、重複したメッセージ参照で受信した最後の部分の時間を確認する必要があります。メッセージ部分の間に 3 日以上ある場合は、新しいメッセージとしてカウントできます。
  7. 最後に、ログ ファイルには並べ替えが必要な行が何十万行もある可能性があるため、これをすべてメモリにロードすることは、おそらくオプションではありません。

入力データの例をいくつか入れて、それをどのように出力する必要があるかを説明するのがおそらく最善です。

入力データ

#message uniqueID,From,To,Time,flag,content,IP,concatenation info   
1,"+1231231234","+15125562100","7 Sep 2012 22:08:33","","abcdefghijklmnopqrstuvwxyz",,
2,"+1231231234","+15125562100","7 Sep 2012 22:08:37","","abcdefghijklmnopqrstuvwxyz",,
3,"+1231231234","+15125562100","7 Sep 2012 22:08:41","","abcdefghijklmnopqrstuvwxyz",,
4,"+8888888888","+15125562100","7 Sep 2012 22:09:01","","SHORTUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wi",,"BQADAQMB  (part 1 of 3 of message reference 1)"
5,"+8888888888","+15125562100","7 Sep 2012 22:09:04","","h my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall ",,"BQADAQMC  (part 2 of 3 of message reference 1)"
6,"+8888888888","+15125562100","7 Sep 2012 22:09:05","","ress, ah, nevermore!",,"BQADAQMD  (part 3 of 3 of message reference 1)"
7,"+8888888888","+15125562100","7 Sep 2012 22:09:06","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIDAQ==  (part 1 of 3 of message reference 2)"
8,"+8888888888","+15125562100","7 Sep 2012 22:09:07",""," my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall p",,"BggEAAIDAg==  (part 2 of 3 of message reference 2)"
10,"+1231231234","+15125562100","7 Sep 2012 22:09:46","","abcdefghijklmnopqrstuvwxyz",,
11,"+1231231234","+15125562100","7 Sep 2012 22:09:50","","abcdefghijklmnopqrstuvwxyz",,
12,"+1231231234","+15125562100","7 Sep 2012 22:09:55","","abcdefghijklmnopqrstuvwxyz",,
13,"+8888888888","+15125562100","13 Sep 2012 22:10:36","","SHORTUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wi",,"BQADAQMB  (part 1 of 3 of message reference 1)"
14,"+8888888888","+15125562100","13 Sep 2012 22:10:38","","h my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall ",,"BQADAQMC  (part 2 of 3 of message reference 1)"
15,"+8888888888","+15125562100","13 Sep 2012 22:10:39","","ress, ah, nevermore!",,"BQADAQMD  (part 3 of 3 of message reference 1)"
16,"+8888888889","+15125562100","7 Sep 2012 22:09:06","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIDAQ==  (part 1 of 3 of message reference 2)"
17,"+8888888889","+15125562100","7 Sep 2012 22:10:42",""," my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall p",,"BggEAAIDAg==  (part 2 of 3 of message reference 2)"
18,"+8888888889","+15125562100","7 Sep 2012 22:10:43","","ess, ah, nevermore!",,"BggEAAIDAw==  (part 3 of 3 of message reference 2)"
19,"+1231231234","+15125562100","13 Sep 2012 20:12:52","","Deposit SMS with readreceiptrequest = false #0",,
20,"+1231231234","+15125562100","13 Sep 2012 20:12:53","","Deposit SMS with readreceiptrequest = false #1",,
21,"+1231231234","+15125562100","13 Sep 2012 20:12:54","","Deposit SMS with readreceiptrequest = false #2",,
22,"+8888888888","+15125562100","13 Sep 2012 20:12:55","","Deposit SMS with readreceiptrequest = false #0: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms ",,"BQADAAMB  (part 1 of 3 of message reference 0)"
23,"+8888888888","+15125562100","13 Sep 2012 20:12:57","","ore; This and more I sat divining, with my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with",,"BQADAAMC  (part 2 of 3 of message reference 0)"
24,"+8888888888","+15125562100","13 Sep 2012 20:12:58","","the lamplight gloating oer She shall press, ah, nevermore!",,"BQADAAMD  (part 3 of 3 of message reference 0)"
25,"+8888888888","+15125562100","7 Sep 2012 22:10:40","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIEAQ==  (part 1 of 2 of message reference 3)"
26,"+8888888888","+15125562100","7 Sep 2012 22:10:42","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIEAQ==  (part 1 of 2 of message reference 3)"
27,"+8888888888","+15125562100","7 Sep 2012 22:10:43","","ess, ah, nevermore!",,"BggEAAIEAw==  (part 2 of 2 of message reference 3)"
28,"+8888888888","+15125562100","13 Sep 2012 20:13:02","","Deposit SMS with readreceiptrequest = false #2: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms ",,"BQADAgMB  (part 1 of 3 of message reference 2)"
29,"+8888888888","+15125562100","13 Sep 2012 20:13:03","","ore; This and more I sat divining, with my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with",,"BQADAgMC  (part 2 of 3 of message reference 2)"
30,"+8888888888","+15125562100","13 Sep 2012 20:13:04","","the lamplight gloating oer She shall press, ah, nevermore!",,"BQADAgMD  (part 3 of 3 of message reference 2)"
31,"+1231231234","+15125562100","13 Sep 2012 20:13:08","","Deposit SMS with readreceiptrequest = true #0",  

出力データ

#message uniqueID,From,To,Time,flag,content,IP,concatenation info   
1,"+1231231234","+15125562100","7 Sep 2012 22:08:33","","abcdefghijklmnopqrstuvwxyz",,
2,"+1231231234","+15125562100","7 Sep 2012 22:08:37","","abcdefghijklmnopqrstuvwxyz",,
3,"+1231231234","+15125562100","7 Sep 2012 22:08:41","","abcdefghijklmnopqrstuvwxyz",,
4,"+8888888888","+15125562100","7 Sep 2012 22:09:01","","SHORTUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wi",,"BQADAQMB  (part 1 of 3 of message reference 1)"
5,"+8888888888","+15125562100","7 Sep 2012 22:09:04","","h my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall ",,"BQADAQMC  (part 2 of 3 of message reference 1)"
6,"+8888888888","+15125562100","7 Sep 2012 22:09:05","","ress, ah, nevermore!",,"BQADAQMD  (part 3 of 3 of message reference 1)"
16,"+8888888889","+15125562100","7 Sep 2012 22:09:06","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIDAQ==  (part 1 of 3 of message reference 2)"
17,"+8888888889","+15125562100","7 Sep 2012 22:10:42",""," my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall p",,"BggEAAIDAg==  (part 2 of 3 of message reference 2)"
18,"+8888888889","+15125562100","7 Sep 2012 22:10:43","","ess, ah, nevermore!",,"BggEAAIDAw==  (part 3 of 3 of message reference 2)"
7,"+8888888888","+15125562100","7 Sep 2012 22:09:06","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIDAQ==  (part 1 of 3 of message reference 2)"
8,"+8888888888","+15125562100","7 Sep 2012 22:09:07",""," my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall p",,"BggEAAIDAg==  (part 2 of 3 of message reference 2)"
10,"+1231231234","+15125562100","7 Sep 2012 22:09:46","","abcdefghijklmnopqrstuvwxyz",,
11,"+1231231234","+15125562100","7 Sep 2012 22:09:50","","abcdefghijklmnopqrstuvwxyz",,
12,"+1231231234","+15125562100","7 Sep 2012 22:09:55","","abcdefghijklmnopqrstuvwxyz",,
25,"+8888888888","+15125562100","7 Sep 2012 22:10:40","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIEAQ==  (part 1 of 2 of message reference 3)"
26,"+8888888888","+15125562100","7 Sep 2012 22:10:42","","LONGUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wit",,"BggEAAIEAQ==  (part 1 of 2 of message reference 3)"
27,"+8888888888","+15125562100","7 Sep 2012 22:10:43","","ess, ah, nevermore!",,"BggEAAIEAw==  (part 2 of 2 of message reference 3)"
19,"+1231231234","+15125562100","13 Sep 2012 20:12:52","","Deposit SMS with readreceiptrequest = false #0",,
20,"+1231231234","+15125562100","13 Sep 2012 20:12:53","","Deposit SMS with readreceiptrequest = false #1",,
21,"+1231231234","+15125562100","13 Sep 2012 20:12:54","","Deposit SMS with readreceiptrequest = false #2",,
22,"+8888888888","+15125562100","13 Sep 2012 20:12:55","","Deposit SMS with readreceiptrequest = false #0: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms ",,"BQADAAMB  (part 1 of 3 of message reference 0)"
23,"+8888888888","+15125562100","13 Sep 2012 20:12:57","","ore; This and more I sat divining, with my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with",,"BQADAAMC  (part 2 of 3 of message reference 0)"
24,"+8888888888","+15125562100","13 Sep 2012 20:12:58","","the lamplight gloating oer She shall press, ah, nevermore!",,"BQADAAMD  (part 3 of 3 of message reference 0)"
28,"+8888888888","+15125562100","13 Sep 2012 20:13:02","","Deposit SMS with readreceiptrequest = false #2: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms ",,"BQADAgMB  (part 1 of 3 of message reference 2)"
29,"+8888888888","+15125562100","13 Sep 2012 20:13:03","","ore; This and more I sat divining, with my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with",,"BQADAgMC  (part 2 of 3 of message reference 2)"
30,"+8888888888","+15125562100","13 Sep 2012 20:13:04","","the lamplight gloating oer She shall press, ah, nevermore!",,"BQADAgMD  (part 3 of 3 of message reference 2)"
31,"+1231231234","+15125562100","13 Sep 2012 20:13:08","","Deposit SMS with readreceiptrequest = true #0",
13,"+8888888888","+15125562100","13 Sep 2012 22:10:36","","SHORTUDH: Thus I sat engaged in guessing, but no syllable expressing To the fowl, whose fiery eyes now burned into my bosoms core; This and more I sat divining, wi",,"BQADAQMB  (part 1 of 3 of message reference 1)"
14,"+8888888888","+15125562100","13 Sep 2012 22:10:38","","h my head at ease reclining On the cushions velvet lining that the lamplight gloated oer, But whose velvet violet lining with the lamplight gloating oer She shall ",,"BQADAQMC  (part 2 of 3 of message reference 1)"
15,"+8888888888","+15125562100","13 Sep 2012 22:10:39","","ress, ah, nevermore!",,"BQADAQMD  (part 3 of 3 of message reference 1)"

私がこれまでやってきたことは、

  1. 時間フィールドをエポック時間に変換して、比較を容易にします
  2. ファイルを読み込む (および書き出す) ことができます。
  3. すべての CSV 列を解析できます。
  4. 連結情報をその部分に分割できます。つまり、8 ビットまたは 16 ビットの参照、部品番号、合計、および参照 ID です。

今、データを効率的にフィルタリングおよびソートするための最良の方法を考え出すことに行き詰まっています。特定のメッセージ参照をソートできるように、ハッシュをいじって最初にファイルをメモリにロードしようとしましたが、大きなファイルでうまくいくかどうかはわかりません。

次に、1 行ずつ読むことを考えましたが、2 行目に連結された SMS の最初の部分が含まれているという問題が発生する可能性があり、ファイルの最後まで後続の部分を取得できない可能性があるため、多分それも良い考えではないと思います。

データベースも考えましたが、これを実行する必要があるシステムにセットアップするには複雑すぎると思います。もう 1 つのオプションは、おそらくパッケージを作成し、複雑な構造をオブジェクトとして保存することでしょうか? おそらく私は物事を複雑にしすぎていますか?私の脳は確かにどろどろになっています!

とにかく、どんなアイデアやガイダンスも大歓迎です。

以上でよろしいかと思いますが、ご不明な点がございましたらお尋ねください。

ありがとう、ウィル

4

2 に答える 2

2

正しく分解すれば、この問題はそれほど複雑ではないと思います。

私が見ているように、ソートプログラムには次の段階が含まれます。

  1. 各行から関連情報を抽出します(タイムスタンプと連結情報)。
  2. メッセージ参照によって行をグループ化します。これは、キャッシュを使用してメモリ効率的に実行できます。
  3. グループをタイムスタンプで並べ替えます。
  4. グループを元の行にフラット化します。

シュワルツ変換

シュワルツ変換は、Perlでソートする場合の一般的なパターンです。すべての比較ではなく、このデータを1回抽出することにより、実際にソートされるデータからソートインデックスを抽出する必要があるソートを高速化します。また、decorate-sort-undecorateと表現することもできます。

例:文字列を長さで並べ替えます。この場合、ナイーブな実装の方が実際には優れていることに注意してください。

my @words = qw( aaa b cccc );
my @sorted_words = 
    map  { $_->[1]             } # flatten
    sort { $a->[0] <=> $b->[0] } # sort by first field (length)
    map  { [ length $_, $_ ]   } # decorate: return arrayref with key and data
    @words;
print "[@sorted_words]\n"; # prints "[b aaa cccc]"

あなたの仕事のためにこのパターンを覚えておくのは良いことです

1.抽出

あなたはすでにそれを管理しました。各行について、次のフィールドを使用して配列参照などを出力します。

0: timestamp (in epoch)
1: part no            \
2: total parts        | these are undef if no concat info is present
3: message reference  /
4: The unmodifed line

CSV抽出の場合はText::CSV、を使用してエポックを計算する必要があります。DateTime

2.グループ化

メッセージ参照をキーとして、グループを値として持つハッシュの形式でキャッシュを定義します。グループは、上記で指定された抽出形式としてのarrayrefですが、位置5以降にさらに行が含まれる場合があります(つまり、タグ付けされた各行はグループです)。

受信したタグ付き回線ごとに、次の手順を実行します。

# pseudocode
# this is how I understood your requirements,
# but it may be wrong. The general principle still holds
# (you may need to choose a different key)
IF the line doesn't have part information, THEN
    pass it on immediately.
ELSE
    IF the hash has an entry for our message reference, THEN
        IF the timestamp of the present group is too old, THEN
            pass on the existing group.
            Add our line for this key.
        ELSE
            Update the group with our line,
            adding the original line (at position 3 + part no),
            but not the metadata to the group.
            IF the group is made complete, THEN
                pass it on immediately,
                delete this entry from the hash.
    ELSE
        Add the line as a group.
        Make sure the content is at position 3 + part no, to allow easy updating.

新しい行がなくなったら、ハッシュ内の残りの各値を次のステージに渡します。

理解しておくべき重要なことは、ここですべての行をメモリに保持する必要はなく、不完全なグループだけを保持する必要があるということです。

興味深いPerl関数はとexists $hash{element}ですdelete $hash{element}deleteメモリを節約するために重要な場合があります。

3.並べ替え

各要素をタイムスタンプで並べ替えるだけです。合計データがシステムで処理するには多すぎる場合は、次のトリックを使用できます。

  1. データの小さなチャンクを並べ替え、ファイルに保存します。
  2. 各ファイルを開きます。
  3. 各ファイルから最初のアイテムをロードします
  4. 実行-少なくとも1つのファイルにアイテムが残っている間:
    1. ロードされたすべてのアイテムを並べ替える
    2. 結果の最初の要素を渡します。
    3. 現在の最初の要素が由来するファイルから次のアイテムをロードします
  5. 他の(すでにロードされている)アイテムを正しい順序で渡します

ただし、これには時間がかかります。

4.平坦化

ここでは、ソートおよびグループ化されたアイテムのみを受け取ります。含まれている行を正しい順序で出力するだけです。

于 2013-02-28T14:00:50.853 に答える
0

これは、メッセージ パーツの結合と並べ替えの 2 つのフェーズで行います。それは問題をいくらか単純化するはずです。

まず、外部の並べ替えユーティリティ (GNU 並べ替えツールなど) を使用して、メッセージ番号で並べ替えます。これにより、少なくとも同じメッセージ番号を持つすべてのパーツがグループ化されます。シンプルsort <inputfile >outputfileはあなたが必要とすることをします。あなたが本当に興味を持っているのは、たとえば、371,"...互いに隣り合っているすべてのパーツを取得することだけです。

次に、出力を読み取り、同じメッセージ番号を持つ行を累積する Perl プログラムを作成できます。別のメッセージ番号が表示されたら、蓄積した行をフィルター処理して、さまざまな部分からメッセージを組み立てます。そして、そのレコードをファイルに書き出します。より簡単にソートできる形式で出力を書きたいと思うかもしれません。おそらく、レコードの先頭にソート対象のフィールドを出力し、ソートを簡素化するために必要に応じてゼロを埋めます。

これが完了すると、1 行に 1 つのレコードを含むファイルが作成されます。レコードを正しく作成sort <inputfile >outputfileした場合は、必要な順序でデータを取得するために別の操作を実行できます。

これにより、プログラミングもかなり簡素化されます。データのカスタム ソートの作成について心配する必要はありません。代わりに、比較的単純な Perl プログラムを作成してデータを変換し、既存のツールでより簡単に並べ替えられるようにします。

于 2013-02-28T03:41:46.553 に答える