0

I am reading in a file: data.txt containing lines like

B07G1W8SHN
B007E2L6G4
B07GXQS5DW
B06XBRP5WY

when I am reading a line of that file and put the variable $line into some context

read line < data.txt
echo "context${line}context"

I get the following output:

contextB07G1W8SHN

what is happening here? I guess there are some hidden delete-characters is my string? How can I read the lines in a variable as a plain string?

xxd data.txt output:

00000000: 4230 3747 3157 3853 484e 0d0a 4230 3037  B07G1W8SHN..B007
00000010: 4532 4c36 4734 0d0a 4230 3747 5851 5335  E2L6G4..B07GXQS5
00000020: 4457 0d0a 4230 3658 4252 5035 5759 0d0a  DW..B06XBRP5WY..
David Ongaro
  • 3,568
  • 1
  • 24
  • 36
Nighel
  • 71
  • 12
  • 1
    Please provide the output of `xxd data.txt` otherwise we'd have to guess what's going on. – David Ongaro Apr 11 '23 at 23:14
  • the output is `00000000: 4230 3747 3157 3853 484e 0d0a 4230 3037 B07G1W8SHN..B007 00000010: 4532 4c36 4734 0d0a 4230 3747 5851 5335 E2L6G4..B07GXQS5 00000020: 4457 0d0a 4230 3658 4252 5035 5759 0d0a DW..B06XBRP5WY` – Nighel Apr 11 '23 at 23:27
  • 3
    Also see [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/q/39527571/4154375) and [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/4154375). – pjh Apr 11 '23 at 23:48
  • 1
    Those `0d0a` sequences in the `xxd` dump are a carriage return (`0d`) and linefeed (`0a`), which is the DOS/Windows convention for line delimiters. You can trim the carriage return in the `read` command with `IFS=$'\r' read line < data.txt`. See the links pjh gave for lots more info and options to deal with this. – Gordon Davisson Apr 12 '23 at 00:39

0 Answers0