From owner-freebsd-questions Wed Feb 26 9:15:10 2003 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9665137B401 for ; Wed, 26 Feb 2003 09:15:08 -0800 (PST) Received: from smtp.infracaninophile.co.uk (happy-idiot-talk.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2B32043FB1 for ; Wed, 26 Feb 2003 09:15:05 -0800 (PST) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1]) by smtp.infracaninophile.co.uk (8.12.7/8.12.7) with ESMTP id h1QHEu5U078203 for ; Wed, 26 Feb 2003 17:14:56 GMT (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.7/8.12.7/Submit) id h1QHEuQO078198 for freebsd-questions@FreeBSD.ORG; Wed, 26 Feb 2003 17:14:56 GMT Date: Wed, 26 Feb 2003 17:14:56 +0000 From: Matthew Seaman To: freebsd-questions@FreeBSD.ORG Subject: Re: cat Message-ID: <20030226171456.GA77795@happy-idiot-talk.infracaninophi> Mail-Followup-To: Matthew Seaman , freebsd-questions@FreeBSD.ORG References: <20030226175613.5e61f45e.tijl@ulyssis.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20030226175613.5e61f45e.tijl@ulyssis.org> User-Agent: Mutt/1.5.3i X-Spam-Status: No, hits=-2.7 required=5.0 tests=IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES,SPAM_PHRASE_03_05, USER_AGENT,USER_AGENT_MUTT version=2.44 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Wed, Feb 26, 2003 at 05:56:13PM +0100, Tijl Coosemans wrote: > I want to remove CRs from text files so what I did is this: >=20 > cat filename | tr -d '\r' > filename >=20 > However, I often end up with an empty file. Just out of > interest, somebody who knows why that is? Hmmm... That's a nasty race condition. In my experience of accidentally doing something like that, it has *always* ended up with an empty file. The problem is that you are attempting both to read =66rom and write to the same file simultaneously. Any shell pipeline of the form: command > filename implies that the shell is going to open 'filename' O_TRUNC (see open(2)) -- hence shortening the file to zero length, before forking and exec'ing the command with its stdout redirected to that filename. Much the same sort of thing can happen when you supply the filename on the command line rather than using the shell redirection operators to supply the data on stdin or stdout. There are commands that can do in-place edits, eg 'perl -i', 'sed -i' (in recent versions of FreeBSD), but these do some sleight of hand behind the scenes to arrange that opening the file for output does not trash it for input, and they certainly won't do in-place edits when used with the shell's redirection operators. The general solution to your problem is always to write to a different file than what you read: tr -d '\r' < filename > filename.n && mv filename.n filename (Note too the influence of the Society for the Suppression of Stray Cats -- save a process today!) Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way PGP: http://www.infracaninophile.co.uk/pgpkey Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message