Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
How can I make iconv replace the input file with the converted output?
Can I read and write to the same file in Linux without overwriting it?

$ cat test.txt 
This is a test file.
$ tr 'a' 'z' < test.txt 
This is z test file.
$ tr 'a' 'z' < test.txt > test.txt
$ cat test.txt
$ 

Why does tr seem to output nothing to test.txt? I found this thread, but I don't really understand what is going on, and how to fix the problem.

(Also, the reason I'm not using sed is because I want to replace newlines with another character, and I don't think sed can operate on a non-line-by-line basis?)

share|improve this question

marked as duplicate by jw013, Michael Mrozek Sep 20 '12 at 18:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

2 Answers 2

You can't redirect both from and to a file, at least not like that. The problem is that when the shell sets up the redirect-to part, it truncates the file. So tr tries to read from the file, its empty.

If your distro has a moreutils package, it contains a sponge program to resolve this problem: tr 'a' 'z' < test.txt | sponge test.txt will work.

Otherwise, you need to write your output to a temporary file, then use mv to overwrite the original file.

share|improve this answer
add comment

The problem is that you are reading and writing to/from the same file at the same time.

Your shell first setups the output redirection which results in an empty file (i.e. truncating it). Then it setups the input redirection and tr reads from an empty file.

You fix it using a temporary file, e.g:

$ tr 'a' 'z' < test.txt > temp.txt && mv temp.txt test.txt
share|improve this answer
add comment

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