他のファイルと比較して発生回数を計算

他のファイルと比較して発生回数を計算

2つのファイルがあります。

F1.txt:

a:erwer|ee
b:eeeee|eee
c:ewrew|erwe

そしてF2.txt

a.T1
a.T2
b.T3
C.T7
c.T4

F2.txtでF1.txtをチェックして、キーワードのa発生b回数を確認する必要があります。c

F1.txtの予想出力:

a:erwer|ee:total:2
b:eeeee|eeet:total:1
c:ewrew|erwe:total:2

他のファイルのo / pを更新します。

a:2
b:1
c:2

ベストアンサー1

ファイルが大きすぎない場合は、awkを使用できます。

awk '
BEGIN{FS=".";OFS=":"}
NR==FNR{a[tolower($0)]=0;next}
{ 
    if(tolower($1) in a){
        a[tolower($1)]++
    }
} 
END{
    for(key in a){
        print key, a[key]
    }
}
' F1.txt F2.txt

大文字と小文字を区別するコンテンツが必要な場合は、そのtolower機能を削除してください。


修正された質問に:

awk '
BEGIN{FS="[:.]";OFS=":"}
NR==FNR{l[tolower($1)]=$0;cpt[tolower($1)]=0;next}
{
    if(tolower($1) in cpt){
        cpt[tolower($1)]++
    }
}
END{
    for(key in cpt){
        print l[key],"total",cpt[key]
    }
}
' F1.txt F2.txt

おすすめ記事