From owner-freebsd-questions@FreeBSD.ORG Tue Feb 17 21:21:38 2004 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 314C716A4CE for ; Tue, 17 Feb 2004 21:21:38 -0800 (PST) Received: from pd2mo1so.prod.shaw.ca (shawidc-mo1.cg.shawcable.net [24.71.223.10]) by mx1.FreeBSD.org (Postfix) with ESMTP id 23A0643D1F for ; Tue, 17 Feb 2004 21:21:38 -0800 (PST) (envelope-from aardvark@saintaardvarkthecarpeted.com) Received: from pd3mr2so.prod.shaw.ca (pd3mr2so-qfe3.prod.shaw.ca [10.0.141.178]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003)) with ESMTP id <0HT900H1ELD9ER@l-daemon> for freebsd-questions@freebsd.org; Tue, 17 Feb 2004 22:17:33 -0700 (MST) Received: from pn2ml6so.prod.shaw.ca (pn2ml6so-qfe0.prod.shaw.ca [10.0.121.150]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003)) with ESMTP id <0HT900FKQLD9EW@l-daemon> for freebsd-questions@freebsd.org; Tue, 17 Feb 2004 22:17:33 -0700 (MST) Received: from francisco.saintaardvarkthecarpeted.com (h24-87-202-31.vc.shawcable.net [24.87.202.31]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003)) with ESMTP id <0HT900K02LD9P6@l-daemon> for freebsd-questions@freebsd.org; Tue, 17 Feb 2004 22:17:33 -0700 (MST) Received: from hardesty.hardesty.saintaardvarkthecarpeted.com (hardesty.saintaardvarkthecarpeted.com [192.168.23.1]) by francisco.saintaardvarkthecarpeted.com (8.12.10/8.12.8) with ESMTP id i1I5IqvR044481; Tue, 17 Feb 2004 21:18:53 -0800 Received: from aardvark by hardesty.hardesty.saintaardvarkthecarpeted.com with local (Exim 3.36 #1 (Debian)) id 1AtK4T-0005rC-00; Tue, 17 Feb 2004 21:16:29 -0800 Date: Tue, 17 Feb 2004 21:16:29 -0800 From: Saint Aardvark the Carpeted In-reply-to: <20040217183030.N52674@sageweb.net> Sender: Debian User To: "Jack L. Stone" Message-id: <20040218051629.GJ2197@hardesty.saintaardvarkthecarpeted.com> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Content-disposition: inline User-Agent: Mutt/1.5.5.1+cvs20040105i References: <20040217183030.N52674@sageweb.net> cc: freebsd-questions@freebsd.org Subject: Re: Using SED in a script 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: Wed, 18 Feb 2004 05:21:38 -0000 Jack L. Stone disturbed my sleep to write: > This would be the steps: > - grep(1) the new string and pipe to sed(1) ..?? > - sed(1) to find the old string & replace with the new string in a file. > Am I on the right track....?? I think so, yeah -- something like this should work: #!/bin/sh new=`grep foo /path/to/bar` old=`cat /path/to/oldvariable` sed -i.bak -e "s/$old/$new/" /file/to/edit Note that I'm using double quotes (") rather than the single quotes (') you usually see with sed scripts; that's so I can use $newvariable and still have the varible substituted in. This assumes there's nothing in $old or $new that would need to be escaped (quotes, slashes, etc). Also, my simplistic example for grep and cat assumes that the product of each is the thing you need to search/replace and nothing else -- if you need the third field (say), look at awk(1). The "-i" option tells sed to edit the file in place, but keep a backup named "/file/to/edit.bak". Another, and maybe more robust approach, to editing the file would be to try Perl, Programming Language of the Elder Gods. (Yeah, I'm a fan. :-). The last line could be replaced by: perl -i.bak -new="$new" -old="$old" -e's/$old/$new/' \ /file/to/edit ...which would be a way of getting difficult values of new and old into single quotes. HTH, Hugh -- Saint Aardvark the Carpeted aardvark@saintaardvarkthecarpeted.com Because the plural of Anecdote is Myth.