From owner-freebsd-questions Sat Jan 22 22:10:28 2000 Delivered-To: freebsd-questions@freebsd.org Received: from yana.lemis.com (yana.lemis.com [192.109.197.140]) by hub.freebsd.org (Postfix) with ESMTP id C365D14D7D for ; Sat, 22 Jan 2000 22:09:54 -0800 (PST) (envelope-from grog@mojave.worldwide.lemis.com) Received: from mojave.worldwide.lemis.com (j39.klt31.jaring.my [161.142.169.233]) by yana.lemis.com (8.8.8/8.8.8) with ESMTP id QAA11627; Sun, 23 Jan 2000 16:38:43 +1030 (CST) (envelope-from grog@mojave.worldwide.lemis.com) Received: (from grog@localhost) by mojave.worldwide.lemis.com (8.9.3/8.9.3) id JAA00717; Sat, 22 Jan 2000 09:23:54 +0530 (IST) (envelope-from grog) Date: Sat, 22 Jan 2000 09:23:54 +0530 From: Greg Lehey To: John Russell Cc: Charles Randall , freebsd-questions@FreeBSD.ORG Subject: Re: UNIX search n replace Message-ID: <20000122092354.D455@mojave.worldwide.lemis.com> Reply-To: Greg Lehey References: <64003B21ECCAD11185C500805F31EC0304D97722@houston.matchlogic.com> <200001211848.TAA24196@gazelle.bigmama.xx> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <200001211848.TAA24196@gazelle.bigmama.xx>; from jr@paranoia.demon.nl on Fri, Jan 21, 2000 at 07:48:47PM +0100 WWW-Home-Page: http://www.lemis.com/~grog X-PGP-Fingerprint: 6B 7B C3 8C 61 CD 54 AF 13 24 52 F8 6D A4 95 EF Organization: LEMIS, PO Box 460, Echunga SA 5153, Australia Phone: +61-8-8388-8286 Fax: +61-8-8388-8725 Mobile: +61-41-739-7062 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Friday, 21 January 2000 at 19:48:47 +0100, John Russell wrote: >> On Friday, January 21, 2000 10:10 AM, Juan Kuuse wrote: >>> >>> This message was sent from Geocrawler.com by "Juan Kuuse" >>> >>> Be sure to reply to that address. >>> >>> More a common UNIX question than a FreeBSD topic: >>> >>> grep >>> to search a keyword multiple files >>> >>> How do I search and replace one keyword for >>> another in multiple files, any suggestions? >> >> Grab a Perl manual and build something along the lines of, >> >>> perl -i.bak -pe 's/word1/word2/g;' Not quite the generic UNIX way. > or use > find . | sed s/word1/word2/g This will change the file names, not their contents, and it won't save it back. You would probably also want to get into the habit of writing quotes around the sed expression, in case it contains delimiters. A better approach to the original question would be: for i in *.c *.h; do sed <$i 's/word1/word2/g' >$i.bak; mv $i.bak $i; done *.c *.h represents the list of files you want to change. You can use any globbing pattern or list of globbing patterns here. You can also perform multiple changes in one pass: use the syntax 's/word1/word2/g; s/word3/word4/g' The 'g' at the end of the expressions means "change every occurrence in the line". If you omit it, you only change the first occurrence. Greg -- Finger grog@lemis.com for PGP public key See complete headers for address and phone numbers To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message