There are hundreds of thousands of files that I need to scan everyday to remove duplicate entries from. Each of these files in turn have several thousand records
Sample input file
2019-10-04,3.9,3.29,5.85,6.15
2019-10-05,3.8,7.02,5.69,6.83
2019-10-05,3.8,8.02,8.69,1.83
2019-10-07,1.8,1.02,4.69,7.83
Here is the script I've written for it which takes about an hour or over to complete.
Script
#!/bin/bash
LOOKUP_DIR="/path/to/source_files"
CLEANEDUP_DIR="/path/to/cleaned_content"
remove_dup(){
fname=${1}
awk -F"," 'prev && ($1 != prev) {print seen[prev]} {seen[$1] = $0; prev = $1} END {print seen[$1]}' "${fname}" > "${CLEANEDUP_DIR}/${fname}"
}
cd ${LOOKUP_DIR}
for k in *.csv
do
remove_dup "${k}" &
done
wait
The way to check duplicates is to look at the first field & if there are multiple entries for this field (date in this case) only the last line with this date needs to be retained & the rest removed.
Is there a way to optimise the logic I've written please?