Shell-Script

比較兩個文件中的兩個欄位,並列印文件中的公共項和每個文件的計數

  • December 11, 2018

我以下面提到的方式有兩個文件

文件 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.

試試這個,

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

引用自:https://unix.stackexchange.com/questions/487314