From owner-freebsd-questions@FreeBSD.ORG Wed Feb 18 06:15:48 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 8AB0816A4CE for ; Wed, 18 Feb 2004 06:15:48 -0800 (PST) Received: from sageweb.net (adsl-65-68-247-74.dsl.crchtx.swbell.net [65.68.247.74]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4F40743D1D for ; Wed, 18 Feb 2004 06:15:48 -0800 (PST) (envelope-from webster@sageweb.net) Received: from sageweb.net (localhost.sageweb.net [127.0.0.1]) by sageweb.net (8.12.8p2/8.12.8) with ESMTP id i1IEFidD055362; Wed, 18 Feb 2004 08:15:45 -0600 (CST) (envelope-from webster@sageweb.net) Received: from localhost (webster@localhost) by sageweb.net (8.12.8p2/8.12.8/Submit) with ESMTP id i1IEFisl055359; Wed, 18 Feb 2004 08:15:44 -0600 (CST) (envelope-from webster@sageweb.net) Date: Wed, 18 Feb 2004 08:15:44 -0600 (CST) From: "Jack L. Stone" To: Saint Aardvark the Carpeted In-Reply-To: <20040218051629.GJ2197@hardesty.saintaardvarkthecarpeted.com> Message-ID: <20040218081341.X55342@sageweb.net> References: <20040217183030.N52674@sageweb.net> <20040218051629.GJ2197@hardesty.saintaardvarkthecarpeted.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=2.2 required=4.5 tests=AWL,MDS_Swen_A_0,MY_OBFUY autolearn=no version=2.63-sageame.rules_v3.1 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 2.63-sageame.rules_v3.1 (2004-01-11) on 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 14:15:48 -0000 On Tue, 17 Feb 2004, Saint Aardvark the Carpeted wrote: > 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 Hugh: Thanks for the reply. This gives me two interesting approaches which should do the job. Appreciate it.....