
我可以使用基本的 shell 工具(沒有 Python 或 Perl)來完成這項工作嗎?
輸入1:
file1.csv
John,Doe,[email protected]
Andy,Barry,[email protected]
Mary,,[email protected]
計算 INPUT2 中第三列 file1 中電子郵件的出現次數:
file2.log
[email protected]&fghfgh
asdda&[email protected]
[email protected]&werewr
期望的輸出:
result.csv
John,Doe,[email protected],0
Andy,Barry,[email protected],2
Mary,,[email protected],1
非常感謝!
答案1
您沒有提供有效的輸入,所以我使用了這個:
John,Doe,[email protected]
Andy,Barry,[email protected]
Mary,,[email protected]
以下 awk 單行語句給出了預期結果:
awk -F, '{l[NR]=$0;f[NR]=$3;c[$3]++}END{for(i=1;i<=NR;i++)print l[i] "," c[f[i]]}'
這裡的問題是您的任務需要兩次傳遞。 (f[] 只是為了避免保留整個內容解析,或在最後重新解析它。)但是因為我不明白為什麼你排除了 python 或 perl (它們是基本的 shell 工具),也許你也不認為 awk 公平遊戲...