From owner-freebsd-questions@FreeBSD.ORG Fri Jan 28 15:54:20 2005 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 B51E016A4CE for ; Fri, 28 Jan 2005 15:54:20 +0000 (GMT) Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.23]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7F36443D2F for ; Fri, 28 Jan 2005 15:54:12 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])j0SFs7Kj024760 for ; Fri, 28 Jan 2005 17:54:09 +0200 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) j0SFs6pP000716 for ; Fri, 28 Jan 2005 17:54:06 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)j0SFs6cR000715 for freebsd-questions@freebsd.org; Fri, 28 Jan 2005 17:54:06 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Date: Fri, 28 Jan 2005 17:54:06 +0200 From: Giorgos Keramidas To: freebsd-questions@freebsd.org Message-ID: <20050128155406.GA659@orion.daedalusnetworks.priv> References: <1098984237.20050128065616@wanadoo.fr> <20050128064057.GB1559@gothmog.gr> <1802109563.20050128120437@wanadoo.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1802109563.20050128120437@wanadoo.fr> Subject: Re: One-line global string replace in all files with sed (or awk?) 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: Fri, 28 Jan 2005 15:54:20 -0000 On 2005-01-28 12:04, Anthony Atkielski wrote: >Giorgos Keramidas writes: >> grep will do. You just have to pass it the right option: >> >> find . -type f | xargs grep -l 'foo' | \ >> xargs sed -i '' -e 's/foo/bar/g' >> >> When passed the -l option (this is a lowercase 'EL'), it will not print >> the matched lines. Only the name of the files that *do* match. Then, >> once you have a list of files that really do match with 'foo' as a >> pattern, you can xargs sed on the list to substitute whatever you want :-) > > It's interesting that there are multiple ways to do this in FreeBSD, > whereas I've never found a way to do it in Windows, even with the fancy > Visual InterDev I've been using for several years (it will _search_ for > strings in multiple files, but it won't do replacements, so you have to > search and then edit each file by hand--try doing this several hundred > times, several times in a row sometime!). I honestly feel pity for the Windows using friends I have in cases like this. A typical example is when they start writing "File Renamers", to mass rename collections of files (i.e. mp3 audio) and start being bitten by bugs in the renamer itself. Another typical example is what I call the robo-user. A person who starts training his fingers and/or brain to repeatedly hit the same sequence of keys in the keyboard, in an effort to rename dozens of files within Explorer. Writing shell scripts to do the job is not necessarily easier, since you have to learn the tools that are available and learn all about the ways they can be glued together, before becoming *really* productive. It pays a thousand times back though, when you start doing in a couple of lines what would take hours of error-prone, manual labor. This is why I have been heard saying: "Unix is for lazy people. So lazy, in fact, that they start learning languages and tricks to avoid as much work as possible, by making their computers do it."