From owner-freebsd-questions@FreeBSD.ORG Thu Jan 12 21:57:16 2006 Return-Path: X-Original-To: freebsd-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 3644E16A434 for ; Thu, 12 Jan 2006 21:57:16 +0000 (GMT) (envelope-from vaaf@broadpark.no) Received: from osl1smout1.broadpark.no (osl1smout1.broadpark.no [80.202.4.58]) by mx1.FreeBSD.org (Postfix) with ESMTP id B7C9843D49 for ; Thu, 12 Jan 2006 21:57:15 +0000 (GMT) (envelope-from vaaf@broadpark.no) Received: from broadpark.no ([217.13.4.96]) by osl1smout1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0IT000A982JWC470@osl1smout1.broadpark.no> for freebsd-questions@freebsd.org; Thu, 12 Jan 2006 23:02:20 +0100 (CET) Received: from [80.202.4.61] (Forwarded-For: [213.187.181.70]) by bgo1mstore1.broadpark.no (mshttpd); Thu, 12 Jan 2006 22:57:23 +0100 Date: Thu, 12 Jan 2006 22:57:23 +0100 From: Kristian Vaaf To: Giorgos Keramidas Message-id: <2f9700a45c66.43c6ded3@broadpark.no> MIME-version: 1.0 X-Mailer: Sun Java(tm) System Messenger Express 6.1 HotFix 0.05 (built Oct 21 2004) Content-type: text/plain; charset=us-ascii Content-language: en Content-transfer-encoding: 7BIT Content-disposition: inline X-Accept-Language: en Priority: normal Cc: lars@gmx.at, freebsd-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: Thu, 12 Jan 2006 21:57:16 -0000 You have been very helpful! I am glad people like you exist to educate those who cannot afford education. All the best in the future, Kristian Vaaf ----- Original Message ----- From: Giorgos Keramidas Date: Thursday, January 12, 2006 2:01 pm Subject: Re: My script to replace strings in ASCII files > On 2006-01-11 21:40, lars wrote: > >Kristian Vaaf wrote: > >> 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. > > It is. You should start using $1, $2, ... for options. > > >> #!/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/ > > Heh, that's nice, and it may even work with multiple files using > vim's -c option in a way similar to sed(1): > > vim -c ':%s/old/new/' file > > > Of course that's not scriptable... > > Using the -c option shown above, it may be scriptable too :) > > > 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. > > This opens a whole can of worms though. What if the user defined > command-line parameters contain special characters (i.e. single > quote, double quotes, etc.)? > > Instead of having to go through all the hoops of parsing quotes > and other special characters in a shell script, and then invoking > sed on each file, passing one file at a time, I prefer using "in > place editing": > > $ grep emacs .bashrc > export EDITOR='nemacs' > alias emacs='nemacs' > > $ perl -pi -e "s/'nemacs'/'emacs'/g" .bashrc > > $ grep emacs .bashrc > export EDITOR='emacs' > alias emacs='emacs' > > $ perl -pi -e "s/'emacs'/'nemacs'/g" .bashrc > > $ grep emacs .bashrc > export EDITOR='nemacs' > alias emacs='nemacs' > > $ > > The number of files passed as 'extra' arguments to perl in this > case is only limited by the amount of text that can fit in a > single command-line, and if that's not enough you can use > xargs(1) to work around the limit: > > find . -type f -name \*foo\* | xargs perl -pi -e "s/old/new/g" > > So, there's no need to write special shell scripts to do this :) > >