Your script works fine. It's the directing the output to the same file it came from which destroys the result. Don't do that.
And your first example does not work. It should be this:
Code:
cat test | sed -e 's/i/I/g' -e 's/n/N/g' > test
And if you try that, you see that it destroys the input as well. Directing out to the same file as the input is wrong.
(The reason why is pretty obvious. Shell pipelines are launched from right to left. The first thing that happens is all the fifos are created. Then sed gets launched, with output directed to 'test'. 'test' is opened in O_WRONLY mode, which destroys it. By the time cat starts up, the input file is already gone)
EDIT: Even if you remove the completely extraneous cat ( cat x | sed -e r can always be replaced with sed -e r x ) it still kills the input, again, because the shell opens the redirect before doing anything else)