From owner-freebsd-questions@FreeBSD.ORG Mon Oct 18 12:48:28 2004 Return-Path: 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 60F3F16A4CE for ; Mon, 18 Oct 2004 12:48:28 +0000 (GMT) Received: from unsane.co.uk (unsane.co.uk [82.152.23.78]) by mx1.FreeBSD.org (Postfix) with ESMTP id A6DB243D2D for ; Mon, 18 Oct 2004 12:48:27 +0000 (GMT) (envelope-from jhary@unsane.co.uk) Received: from unsane.co.uk (localhost [127.0.0.1]) by unsane.co.uk (8.13.1/8.12.10) with ESMTP id i9ICmMue064345 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 18 Oct 2004 13:48:22 +0100 (BST) (envelope-from jhary@unsane.co.uk) Received: from localhost (jhary@localhost) by unsane.co.uk (8.13.1/8.12.10/Submit) with ESMTP id i9ICmMP8064342; Mon, 18 Oct 2004 13:48:22 +0100 (BST) (envelope-from jhary@unsane.co.uk) Date: Mon, 18 Oct 2004 13:48:22 +0100 (BST) From: Vince Hoffman To: Richard Bradley In-Reply-To: <200410181334.37665.rtb27@cam.ac.uk> Message-ID: <20041018134625.F63651@unsane.co.uk> References: <200410181334.37665.rtb27@cam.ac.uk> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed cc: freebsd-questions@freebsd.org Subject: Re: How to run a stream based command in place on a file X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Oct 2004 12:48:28 -0000 On Mon, 18 Oct 2004, Richard Bradley wrote: > Hi, > > I want to run stream based commands like `sed` and `tr` on the contents of a > file, and save the results to the same file. > > Obviously I can do this with a temporary file: > > $sed s/dog/cat/ myanimals.txt > tmp.txt > $mv tmp.txt myanimals.txt > > But is there any way I can do this with a single command? Not sure about tr but sed does give the -i option to edit in place. I'm not sure about a more general solution though. > > My first guess would be a "buffer" command that reads a file into memory (or > into a temp file) then pipes it to stdout, e.g. > > $cat myanimals.txt | buffer | sed s/dog/cat/ > myanimals.txt > > But there isn't one which, in my experience of BSD, means it either wouldn't > work or there is a better way to do it :-) > > Having read through the Bash manual and run some experiments, it seems that > the ">" operator truncates an output file to zero length before any commands > are run. > > So my missing command becomes: > > $cat myanimals.txt | sed s/dog/cat | bufferedwrite myanimals.txt > > I can't find anything like this anywhere -- any ideas what the "proper" way to > do this is? > > Thanks in advance, > > > Rich > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" >