私は perl の経験がほとんどなく、完了しなければならない簡単なタスクがあります。文字列のリストがあります。文字列の最初の部分はファイル名で、文字列の 2 番目の部分はアンダースコアで区切られたバージョン番号です。各ファイルの最新バージョンを含む文字列を出力したいと思います。
たとえば、次のデータ セットを使用します。
P0689.D130624.C01_0
P0689.D130624.C02_0
P0689.D130624.C04_0
P0689.D130624.C05_0
P0730.D130624.C01_0
P0730.D130624.C01_1
P0730.D130624.C01_2
P0730.D130624.C01_0
P0730.D130624.C01_1
私の出力は次のようになります。
P0689.D130624.C01_0
P0689.D130624.C02_0
P0689.D130624.C04_0
P0689.D130624.C05_0
P0730.D130624.C01_2
これを行う最も簡単な方法は、ファイル名をマップのキーとして分割し、現在の最大値である場合はバージョンを値として挿入することだと考えました. 次に、ハッシュを反復処理し、キーと値のペアを再結合すると、結果が得られます。ただし、ハッシュを反復処理するたびに、キーは一意ではないようです。バージョンは常に昇順であるため、最終的には初期データ セット全体になります。これはおそらく、perl 構文に関する私の経験不足の結果ですが、助けていただければ幸いです。
これが私の現在のコードです:
my %hash = ();
foreach my $line (@F) {
my @splt = split('_', $line);
my $key = $splt[0];
my $val = $splt[1];
unless (exists $hash{$key} and $hash{$key} >= $val) {
$hash{$key} = $val;
}
};
while(my ($k,$v) = each %hash ) {
print $k . '_' . $v, "\n";
};
編集 (2): 出力データの詳細。
このスクリプトを使用してデモを行います。
#!/bin/bash
DIR="/app/jobsandservices/shareddata/nphdocp1/2013/06/24/"
FILTER="[Pp]\(0689\|1391\|0730\)"
ls -1 $DIR*
echo -----
ls -1 $DIR* | grep "$FILTER"
echo -----
ls -1 $DIR* | grep "$FILTER" | perl -an latest-version.pl
これは端末からの正確な出力です:
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/CONFIRM.C0668.S01125_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/CONFIRM.C0668.S26599_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0188.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0212.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0701.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0702.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0706.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0706.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0709.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0821.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0822.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0823.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0824.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0825.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2026.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2029.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2070.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2084.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2156.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2172.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2193.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2202.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2203.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2362.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2363.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2363.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2364.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2365.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2429.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2518.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C24_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C24_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2658.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2659.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2660.D130624.C00_0
-----
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0
-----
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0
これも perl v5.10.1 を使用しているため、環境に問題がある可能性があります。