From owner-freebsd-questions Fri May 31 16:51:26 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mail.halplant.com (ip68-100-145-31.nv.nv.cox.net [68.100.145.31]) by hub.freebsd.org (Postfix) with ESMTP id 0493737B409 for ; Fri, 31 May 2002 16:51:23 -0700 (PDT) Received: by mail.halplant.com (Postfix, from userid 1001) id 0BB0F1F4; Fri, 31 May 2002 19:51:22 -0400 (EDT) Date: Fri, 31 May 2002 19:51:21 -0400 From: Andrew J Caines To: freebsd-questions@freebsd.org Subject: Re: Why does 'sed' delete my input file? Message-ID: <20020531235121.GK32486@hal9000.halplant.com> Reply-To: Andrew J Caines Mail-Followup-To: freebsd-questions@freebsd.org References: <20020531130029.B28925@dogma.freebsd-uk.eu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020531130029.B28925@dogma.freebsd-uk.eu.org> User-Agent: Mutt/1.3.99i Organization: H.A.L. Plant X-PGP-Fingerprint: C59A 2F74 1139 9432 B457 0B61 DDF2 AA61 67C3 18A1 X-Powered-by: FreeBSD 4.6-RC X-URL: http://halplant.com:88/ Importance: Normal 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 There have been quite a few suggestions how best to do this, most of which should work, however I suggest the most general functional solution is as follows: # for file in file1 file2 file3 ... do cp $file $file.x && sed 's/re/pattern/f' $file.x > $file && rm $file.x done This can be trivially set up as a function. This has several advantages: you keep the file mode and CTIME of $file, which doesn't necessarily happen with mv; you have a copy of the original if the sed fails; it scales to multiple files; it can be made parallel easily with job control and it works with full (or partly qualified) paths (unlike when you use /tmp or similar). -Andrew- -- _______________________________________________________________________ | -Andrew J. Caines- Unix Systems Engineer A.J.Caines@halplant.com | | "They that can give up essential liberty to obtain a little temporary | | safety deserve neither liberty nor safety" - Benjamin Franklin, 1759 | To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message