Shell Scripting unwanted '?' character at the end of the file name

Am I getting an unwanted '?' at the end of my file name with this:

emplid=$(grep -a "Student ID" "$i".txt | sed 's/(Student ID: //g' | sed 's/)Tj//g' ) #gets emplid by doing a grep from some text file echo "$emplid" #prints employee id correctly cp "$i" "$emplid".pdf #getting an extra '?' character after emplid and before .pdf 

ie instead of getting a file name like 123456.pdf, I get 123456? .pdf. Why does this happen if echo printing is correct? How to remove trailing question mark characters?

+4
source share
2 answers

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.

+7
source

using the file command to determine if it is pure unix or mixed with DOS.

DOS file: ASCII text, with CRLF line terminators

Unix file is a pure ASCII file.

0
source

Source: https://habr.com/ru/post/1444440/


All Articles