I am trying to do the following with a sed script, but it takes too much time. It seems like I'm doing wrong.
Scenario: I have student records (> 1 million) in students.txt
. In this file (each line), the 1st 10 characters are the student ID, and the next 10 characters are the contact number, etc.
students.txt
1000000001 9234567890 XXX ...
1000000002 9325788532 YYY ...
.
.
.
1001000000 8766443367 ZZZZ ...
I have another file (encrypted_contact_numbers.txt) that has all the phone numbers, and the numbers and corresponding encrypted phone numbers are below
encrypted_contact_numbers.txt
Phone_Number, Encrypted_Phone_Number
9234567890, 1122334455
9325788532, 4466742178
.
,
.
8766443367, 2964267747
I wanted to replace all contact numbers (11th position 20) students.txt
with the corresponding encrypted phone number from encrypted_contact_numbers.txt
.
Expected Result:
1000000001 1122334455 XXX ...
1000000002 4466742178 YYY ...
,
,
,
1001000000 2964267747 ZZZZ ...
I am using the sed script below to perform this operation. It works fine, but too slow.
Approach 1:
while read -r pattern replacement; do
sed -i "s/$pattern/$replacement/" students.txt
done < encrypted_contact_numbers.txt
Approach 2:
sed 's| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|' <encrypted_contact_numbers.txt |
sed -f- students.txt > outfile.txt
Is there a way to quickly process this huge file?
: 9 2018
, AWK Perl, , ( 10-20). , . ?
students.txt:
1000000001 9234567890 XXX... 9234567890
1000000002 9325788532 YYY...
.
.
.
1001000000 8766443367 ZZZZ 9234567890...