From owner-freebsd-questions Fri May 31 5:11:53 2002 Delivered-To: freebsd-questions@freebsd.org Received: from maila.telia.com (maila.telia.com [194.22.194.231]) by hub.freebsd.org (Postfix) with ESMTP id 89A3737B401 for ; Fri, 31 May 2002 05:11:47 -0700 (PDT) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by maila.telia.com (8.11.6/8.11.6) with ESMTP id g4VCBjo08141 for ; Fri, 31 May 2002 14:11:46 +0200 (CEST) Received: from falcon.midgard.homeip.net (h53n2fls20o913.telia.com [212.181.163.53]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id OAA16321 for ; Fri, 31 May 2002 14:11:44 +0200 (CEST) Received: (qmail 696 invoked by uid 1001); 31 May 2002 12:11:44 -0000 Date: Fri, 31 May 2002 14:11:43 +0200 From: Erik Trulsson To: j mckitrick Cc: freebsd-questions@freebsd.org Subject: Re: Why does 'sed' delete my input file? Message-ID: <20020531121143.GA684@falcon.midgard.homeip.net> Mail-Followup-To: j mckitrick , 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 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 Fri, May 31, 2002 at 01:00:29PM +0100, j mckitrick wrote: > > This is a simple question, but I can't find the answer. The Daemonnews > article that seems to answer it is missing the graphics with the > screenshots. > > If I want to replace all occurrences of 'foo' in a file, this is what I > tried: > > sed s/foo/bar/g file1 > file1 > > But this deletes (overwrites?) the contents of the file. What did I do > wrong? > > Thanks... > > NOTE: Please CC me, as I am not currently subscribed. Thanks. It is not sed(1) that deletes your file. It is the shell. When you redirect the output to file (as you do with '> file1' above) the shell creates that file (or truncates the old file if it already exists) before the command is executed, so when sed opens the file it is already empty. What you will have to do is essentially: mv file1 file1.bak sed s/foo/bar/g file1.bak > file1 rm file1.bak (Assuming that the sed syntax is correct. I am no expert on sed.) If you use perl instead of sed you can do it in one line: perl -pi -e 's/foo/bar/g' file1 -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message