Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Jan 2006 21:40:39 +0100
From:      lars <lars@gmx.at>
Cc:        questions@freebsd.org
Subject:   Re: My script to replace strings in ASCII files
Message-ID:  <43C56D47.1040809@gmx.at>
In-Reply-To: <f377343c7093.43c57793@broadpark.no>
References:  <f377343c7093.43c57793@broadpark.no>

next in thread | previous in thread | raw e-mail | index | archive | help
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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?43C56D47.1040809>