1

I'm reading a single-cell csv file and printing the value as a string with some other strings attached to it. It does not work for certain combinations. Do you know why?

 while IFS="," read ts
 do
     echo "timestamp is: $
     tstamp=$ts
 done < timestamp.csv

 a="TS_"
 c="234"
 b=".log"
 d="${a}${c}${b}"
 Log_1="${a}$tstamp"
 Log_2="${tstamp}${a}"
 echo "$d"
 echo "$Log_1"
 echo "$Log_2"

actual result:

timestamp is: 0x5cff71d8

TS_234.log

TS_0x5cff71d8

TS_cff71d8

expected result:

timestamp is: 0x5cff71d8

TS_234.log

TS_0x5cff71d8

0x5cff71d8TS_

1 Answer 1

1

The CSV file was made on a Windows machine and each line ends with a CR ('\r', '\x0D') character followed by a LF ('\n', '\x0A') character; you are reading the file on a Linux or Unix or MacOS machine, where lines are supposed to end with a bare LF character.

CR is Carriage Return: printing this character moves the cursor back to the beginning of the line.

Your $Log_2 variable contains 0 x 5 c f f 7 1 d 8 \r T S _, which, when printed looks like TS_cff71d8:

0x5cff71d8\r (now cursor moves back to the beginning of the line)
TS_ (overwriting the first three characters)

To check, try echo "$Log_2" | od -A x -t x1z -v (or whatever options for od work on your system).

To get rid of the MS-DOS-ish CR characters, you may have a utility on your system named dos2unix; or you may try sed -e 's/\r//g'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I tested the "sed" and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.