From owner-freebsd-questions@FreeBSD.ORG Mon Oct 18 12:35:41 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 F1ED216A4CE for ; Mon, 18 Oct 2004 12:35:41 +0000 (GMT) Received: from ppsw-5.csi.cam.ac.uk (ppsw-5.csi.cam.ac.uk [131.111.8.135]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6F35843D31 for ; Mon, 18 Oct 2004 12:35:41 +0000 (GMT) (envelope-from rtb27@cam.ac.uk) Received: from rtb27.robinson.cam.ac.uk ([131.111.236.214]:49740) by ppsw-5.csi.cam.ac.uk (smtp.hermes.cam.ac.uk [131.111.8.155]:465) with esmtp (SSLv3:RC4-MD5:128) (Exim 4.34) id 1CJWja-0002b1-8Q (return-path ) for freebsd-questions@freebsd.org; Mon, 18 Oct 2004 13:35:30 +0100 From: Richard Bradley To: freebsd-questions@freebsd.org Date: Mon, 18 Oct 2004 13:34:37 +0000 User-Agent: KMail/1.5.4 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200410181334.37665.rtb27@cam.ac.uk> X-Cam-ScannerInfo: http://www.cam.ac.uk/cs/email/scanner/ X-Cam-AntiVirus: No virus found X-Cam-SpamDetails: Not scanned Subject: 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:35:42 -0000 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? 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