比较两个文件中的两个字段,并打印文件中的公共项以及每个文件中的计数

比较两个文件中的两个字段,并打印文件中的公共项以及每个文件中的计数

我有两个文件,如下所示

文件1

Emp1 Dec10 12Am 
Emp2 Dec10 2Am 
Emp3 Dec10 4Am 
Emp2 Dec10 6Am 
Emp1 Dec10 8Am 

文件2

Emp1 Dec11 12Pm 
Emp2 Dec11 2Am 
Emp1 Dec11 3Am 
Emp3 Dec11 4Am 
Emp1 Dec11 5Am 

所以我试图将输出设为

Emp1 presence on Dec10 was 2 times and on Dec11 was 3 times. 
Emp2 presence on Dec10 was 2 times and on Dec11 was 1 time. 
Emp3 presence on Dec10 was 1 time and on Dec11 was 1 time.

答案1

尝试这个,

EMP=(`awk '!seen[$1]++ {print $1}' File1 File2`)

for emp in "${EMP[@]}"
do
    DAYS=`awk -v b=$emp '$1==b {print $2}' File1 File2 | sort | uniq -c | awk '{print $2" was "$1" times"}' | sed ':a;N;$!ba;s/\n/ and on /g'`
    echo "$emp presence on $DAYS "
done

相关内容