Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Mar 2003 00:58:25 -0800 (PST)
From:      Viktor Lazlo <viktorlazlo@telus.net>
To:        Tijl Coosemans <tijl@ulyssis.org>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: cat
Message-ID:  <20030304133800.T89904-100000@a3ij25fvy80j.bc.hsia.telus.net>
In-Reply-To: <20030226175613.5e61f45e.tijl@ulyssis.org>

next in thread | previous in thread | raw e-mail | index | archive | help


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 majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030304133800.T89904-100000>