Linuxでさまざまな値の数を取得する必要があります

Linuxでさまざまな値の数を取得する必要があります
aff_id=752&off_id=4503trans_id=acacthf60cxr
aff_id=752&off_id=4553trans_id=acacthf60cxr
aff_id=752&off_id=4543trans_id=acacthf60cxr
aff_id=752&off_id=4543trans_id=acacthf60cxr
aff_id=752&off_id=4553trans_id=acacthf60cxr
aff_id=752&off_id=4503trans_id=acacthf60cxr
aff_id=752&off_id=4513trans_id=acacthf60cxr
aff_id=752&off_id=4513trans_id=acacthf60cxr
aff_id=752&off_id=4503trans_id=acacthf60cxr

これは、同じaff_idとは異なる「off_id」を持つ私のファイル形式です。 「off_id」に基づいて数を取り出す必要があります。

ベストアンサー1

アッ解決策:

awk -F'[=&]' '{ a[substr($4,0,4)]++ }END{ for(i in a) print i,a[i] }' file
  • -F'[=&]'- 複雑なフィールド区切り記号

  • a[substr($4,0,4)]++- 値は同様の形式off_idで4番目のフィールドにあるため、必要な値が抽出されます。配列は、発生回数を値としてインデックス化されます。$44503trans_id
    substr($4,0,4)4503
    aoff_id


出力:

4503 3
4513 2
4543 2
4553 2

おすすめ記事