On Wed, 26 Feb 2003, Tijl Coosemans wrote:

> I want to remove CRs from text files so what I did is this:
>
> cat filename | tr -d '\r' > filename
>
> However, I often end up with an empty file. Just out of
> interest, somebody who knows why that is?

This is because prior to any other action the shell opens the file for
writing, in effect creating a new empty file to act as the input for any
other operations.

Some utilities like sort provide their own solutions to let you use the
same file as both input and output, but most require an alternate
strategy.

The most common method is to use two files to avoid having the original
overwritten until all operations are complete:

utility -flags file.1 >file.2 && mv file.2 file.1

Alternatively you can replace the intermediate files with standard
output/input to allow the original file to be replaced with the output of
the command you wish to run:

 cp file - | (utility -flags <- >file)

Cheers,

Viktor



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-questions" in the body of the message

Reply via email to