Sed not working as intended

sed

I would like to use this script with the intention of replacing all instances of the letter Q within a file with the contents of the file "question.txt". Instead what happens is that all the instances of Q in the file disappear and the contents of "question.txt" are printed at the bottom of the new file.

Any ideas?

sed -i.bkp '/Q/{
s/Q//g
r /Users/ericbrotto/Desktop/question.txt
}' Commision.txt 

EDIT 1

I'm trying to find out what version I have, but keep on getting this:

enter image description here

Best Answer

I guess I found the reason why.

If I put some blank after the filename, a filename with trailing blanks is searched. I can reproduce the problem this way:

Sources:

cat q.dat
Q
Not q
And Q again
And again not


cat kuh.dat 
Die dumme
Kuh

Working example with filename 'kuh.dat':

sed '/Q/{
s/Q//g
r kuh.dat
}' q.dat

Die dumme
Kuh
Not q
And  again
Die dumme
Kuh
And again not

Now the failing example, with 'kuh.dat '.

sed '/Q/{
s/Q//g
r kuh.dat 
}' q.dat

Not q
And  again
And again not

Since the filename isn't quoted, it is a great surprise for me, that the blank at the filename-end is recognized. And it isn't visible in the shell, so I searched for a long time without success, where the difference between the two examples is.

Related Question