From owner-freebsd-questions@FreeBSD.ORG Wed Jan 11 20:40:40 2006 Return-Path: X-Original-To: questions@freebsd.org 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 14D3616A41F for ; Wed, 11 Jan 2006 20:40:40 +0000 (GMT) (envelope-from lars@gmx.at) Received: from mail.gmx.net (mail.gmx.net [213.165.64.21]) by mx1.FreeBSD.org (Postfix) with SMTP id 32F8B43D4C for ; Wed, 11 Jan 2006 20:40:38 +0000 (GMT) (envelope-from lars@gmx.at) Received: (qmail invoked by alias); 11 Jan 2006 20:40:37 -0000 Received: from 5.55.79.83.cust.bluewin.ch (EHLO [192.168.1.10]) [83.79.55.5] by mail.gmx.net (mp035) with SMTP; 11 Jan 2006 21:40:37 +0100 X-Authenticated: #912863 Message-ID: <43C56D47.1040809@gmx.at> Date: Wed, 11 Jan 2006 21:40:39 +0100 From: lars User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051215) X-Accept-Language: en-us, en MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 Cc: questions@freebsd.org Subject: Re: My script to replace strings in ASCII files X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Jan 2006 20:40:40 -0000 Kristian Vaaf wrote: > Hello! > > Just curious, what do I need to do to be able to execute this script like: > > $ text-replace old_string new_string > > I find it a bit inconvenient having to edit the script for every thing I need to replace. > > Thanks, > Kristian > > #!/usr/local/bin/bash > # > # Replace old with new inside all text files. > # $URBAN: text-replace.sh,v 1.0 2005/10/24 15:09:05 vaaf Exp $ > # > > for file in `find . -type f ! -name ".*"`; do > > if [ "`file -b "$file" | grep text`" != "" ]; then > > sed -i '' "s/old/new/g" "$file" > > echo "$file: Done" > > fi > > done Why not open the file with vim and then :.,$s/old/new/ Of course that's not scriptable... Maybe you should interpolate the first and the second argument into your regexes in the substitution with sed, so you get "s/argument1/argument2/" Perl might help though.