I have two big files with more than six million records. Data in those two files can be correlated by UID (if ordering the file, should be at same row in both files). Eventually I need to get data from the first file concatenated with data in the second file.
The issue is that executing the script is taking 10 hours for about 650,000 records!!
I'd like to improve it.
UIDS=`cut -f1 -d',' sorted_UID_data1.txt`
for record in $UIDS
do
echo `grep $record sorted_UID_data1.txt| awk -F ',' '{print $2}'`,`grep $record sorted_UID_data2.txt` >> data.txt
done
In order to optimize it, I thought of
TOTAL_RECORDS=`wc -l < sorted_UID_data1.txt`
recordId=1
while [ $recordId -le $TOTAL_RECORDS ]
do
echo `sed -n "${recordId}{p;q;}" sorted_UID_data1.txt| awk -F ',' '{print $2}'`,`sed -n "${recordId}{p;q;}" sorted_UID_data2.txt` >> data.txt
recordId=$(( $recordId + 1 ))
done
And this is also taking too much time.
But then, I'm thinking: What if I always can grab the first line of the file? I've seen that this could be done by sed, tail, or AWK, but this seems to be inefficient.
How can I fix this problem?
join
orpaste
could handle, you could read all the lines of one file into an associative array in awk, or a list or hash in perl, then process the other file and grab appropriate entries from the first file. If they're both sorted,perl
is a good choice for reading from two files at once. I think awk makes that fairly easy, too, but you'd have to process it withsplit
manually, not awk field splitting.