1

I am extracting a string variable from a file and then print it to the screen. The problem is that using just echo works, but if I used echo -n, then nothing is printed. Here is an example:

$ Var=$(grep -a MrEdbType FileName)
$ echo $Var
Prot
$ echo -n $Var
$ 

Similarly, if I use printf, it works only if I add \n:

$ Var=$(grep -a MrEdbType FileName)
$ printf "%s" $Var
$
$ printf "%s\n" $Var
Prot

What is wrong with the string I am extracting? The file is a DICOM file containing a mixture of text and binary data, hence the use of -a option.

As suggested in the comments, using hexdump -C option generated this result:

$ Var=$(grep -a MrEdbType FileName | hexdump -C)
$ echo $Var
00000000 50 72 6f 74 0d 0a |Prot..| 00000006
Renat
  • 223
  • 3
  • 8

1 Answers1

0

A suggested link led to an answer - using sed 's/\r$//' at the end of the grep search fixed the problem!

Renat
  • 223
  • 3
  • 8