Date: Fri, 31 May 2002 14:11:43 +0200 From: Erik Trulsson <ertr1013@student.uu.se> To: j mckitrick <jcm@FreeBSD-uk.eu.org> Cc: freebsd-questions@freebsd.org Subject: Re: Why does 'sed' delete my input file? Message-ID: <20020531121143.GA684@falcon.midgard.homeip.net> In-Reply-To: <20020531130029.B28925@dogma.freebsd-uk.eu.org> References: <20020531130029.B28925@dogma.freebsd-uk.eu.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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 -- <Insert your favourite quote here.> Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020531121143.GA684>