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

link|improve this question
3  
I can't reproduce this. Post a sample input file and the output you get, and tell us what sed version you're using. – Gilles Jul 25 '11 at 12:57
1  
I can reproduce it with GNU sed-Version 4.2.1, without the content of 'question.txt' printed at the bottom of the new file. – user unknown Jul 26 '11 at 1:01
How does one find out which version they have? I did sed -V and it didn't work :( – Eric Brotto Jul 26 '11 at 8:34
I reproduced this as well with GNU sed version 4.2.1. A simple set of test files where the search string was in the middle of the file left the read file at the end, not in place of the searched string. – Caleb Jul 26 '11 at 8:35
@Eric: sed --version – Caleb Jul 26 '11 at 8:35
show 7 more comments
feedback

1 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.

link|improve this answer
Good idea. It doesn't explain why there'd be a copy of question.txt at the bottom of the output though. – Gilles Jul 28 '11 at 16:33
Ah, yes, I had forgotten about that. And I have never reproduced it. Did somebody reproduce that? – user unknown Jul 28 '11 at 17:20
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.