It looks like your script file has the end of the line in DOS style (\ r \ n) instead of the unix style (just \ n) - when the script is in this format, \ r is considered as part of the commands. In this case, it is included in $ emplid and therefore in the file name.
Many platforms support the dos2unix command to convert a file to the end of a line in unix style. And once it is converted, use text editors that support unix-style text files.
EDIT: I assumed that the end of the line of the problem was in the shell of the script, but it looks like they are in the input file ("$ i" .txt). You can use dos2unix in the input file to clear it and / or add a cleanup step to the sed command in your script. BTW, you can have one sed instance applying a few changes with the -e option:
emplid=$(grep -a "Student ID" "$i".txt | sed '-es/(Student ID: //g' -e 's/)Tj//g' -e $'s/\r$//' )
I would recommend not using sed 's/.$//' - if the file is in unix format, which will disable the last character of the file name.
source share