Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 31 May 2002 14:14:21 +0200
From:      Benoit Lacherez <blacherez@ac-bordeaux.fr>
To:        j mckitrick <jcm@FreeBSD-uk.eu.org>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Why does 'sed' delete my input file?
Message-ID:  <20020531141421.A72043@bisclavret.iris33.ac-bordeaux.f>
In-Reply-To: <20020531130029.B28925@dogma.freebsd-uk.eu.org>; from jcm@FreeBSD-uk.eu.org on Fri, May 31, 2002 at 01:00:29PM %2B0100
References:  <20020531130029.B28925@dogma.freebsd-uk.eu.org>

next in thread | previous in thread | raw e-mail | index | archive | help
j mckitrick a écrit :
> 
> 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?

If you redirect your command output to file1, this file is first
opened in write mode (and so cleared) then opened in read mode, but at
that time, it's already empty...

The solution is to read the file first, then to write in it :

cat file1 | sed 's/foo/bar/g' > file1


-- 
Benoit Lacherez
Académie de Bordeaux -- CATICE
Projet de traduction de la documentation de Python:
http://frpython.sourceforge.net/

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?20020531141421.A72043>