From owner-freebsd-questions Wed Nov 11 21:15:24 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id VAA25922 for freebsd-questions-outgoing; Wed, 11 Nov 1998 21:15:24 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from pobox.com (minbar-2-110.mdm.mke.execpc.com [169.207.135.238]) by hub.freebsd.org (8.8.8/8.8.8) with SMTP id VAA25899 for ; Wed, 11 Nov 1998 21:15:00 -0800 (PST) (envelope-from hamilton@pobox.com) Message-Id: <199811120515.VAA25899@hub.freebsd.org> Received: (qmail 11322 invoked from network); 11 Nov 1998 23:14:46 -0600 Received: from localhost (HELO pobox.com) (127.0.0.1) by localhost with SMTP; 11 Nov 1998 23:14:46 -0600 To: Rick Aliwalas cc: "Alain G. Fabry" , freebsd-questions@FreeBSD.ORG Subject: Re: find and replace within files. In-reply-to: Your message of "Wed, 11 Nov 1998 14:10:25 EST." Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 11 Nov 1998 23:14:46 -0600 From: Jon Hamilton Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In message , Rick Aliwalas wrote: } On Wed, 11 Nov 1998, Alain G. Fabry wrote: } } > Is there any way in FreeBSD that I can search in files for certain lines an } d } > replace them with something else? } > f.e. search for Name and replace with Last_Name } > This needs to be done thruogh a whole directory tree. } > Can you refer me to a command which will do this? } > Thanks, } > } > Alain } } The below script will change all instances of the string "blue apple" } to "red pear" in files under the directory /home/zzz. This may or may } not help you write your own script. Make sure you backup your files and } test your script on a bogus directory first! Be aware that it will also mangle permissions and will fail for filenames with whitespace in them. This kind of thing is relatively straightforward with perl5; if you'd prefer to stick to "stock" tools, an ed script would probably be more advisable if you care about these issues. } #!/bin/sh } for file in `find /home/zzz` } do } if [ -f $file ] } then } sed 's/blue apple/red pear/g' $file > /tmp/$$ } mv /tmp/$$ $file } fi } done -- Jon Hamilton hamilton@pobox.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message