Rather than invoking echo 40,000 times (when none are needed), invoking cat 40,001 times (when none are needed), invoking tee 40,000 times (when none are needed), and invoking sed 40,000 times; I would prefer to just invoke awk once. That should speed up your script by a few orders of magnitude.
I am making a (not too wild) assumption that your sdn file was created with an editor or a DOS/Windows utility that uses <carriage-return><newline> character pairs as the line terminator instead of the single <newline> character line terminator expected on UNIX systems, Linux systems, and other similar systems (which is causing the ? in the filenames you're creating when displayed by some versions of the ls utility as an indication that there is a control character at that point in the filename). I'm also making the wild assumption that you want that extraneous <carriage-return> character in all of the output files you're creating if it also exists in your template file. With those assumptions, try changing your script to be:
#!/bin/bash
IMPORT="/home/ric/sdn.txt"
TEMPLATE="/home/ric/template.txt"
awk '
NR == 1 {
IN = $0
next
}
{ OUT = IN
sub(/\r/, "", $1)
file = "/home/ric/" $1 ".txt"
gsub(/62XX/, $1, OUT)
print OUT > file
close(file)
}' "$TEMPLATE" "$IMPORT"
If sdn.txt contains:
62387626512
62483736261
628372726392
623982736362
with or without <carriage-returns> and template.txt contains:
62XX@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62XX&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62XX&id=88762XX&TxtType=0&code=hello_66517
it produces the output files:
62387626512.txt containing:
62387626512@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62387626512&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62387626512&id=88762387626512&TxtType=0&code=hello_66517
62483736261.txt containing:
62483736261@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=62483736261&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=62483736261&id=88762483736261&TxtType=0&code=hello_66517
628372726392.txt containing:
628372726392@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=628372726392&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=628372726392&id=887628372726392&TxtType=0&code=hello_66517
and 623982736362.txt containing:
623982736362@xxxx://xxx.xxx.xxx.xxx:xxxx/Uid=xxxx&Pwd=xxxx&id=7034056003&sdn=623982736362&Txt=Hallo World ?? sent to click xxxx://budi.com/games/m=623982736362&id=887623982736362&TxtType=0&code=hello_66517
which seems to be what you're trying to do.