Re: Help: Command to search & replace word in file?
On Thu, Oct 19, 2000 at 09:26:59AM +0200, Jonathan Gift wrote:
> Is there a way from the console to use a grep like command to search a file
> for, say the word "blue" and replace it with "red"? The key is console and
> not within Vim, etc. I'm trying to set up a way to automate changing
> .Xdefault setups with a simple Bash script.
>
I use this here once in a while for such tasks:
---------- snip --------------
#!/bin/sh
# $Id: changer,v 1.1 1996/11/23 12:26:21 sami Exp sami $
# Sami Lempinen * ssl@sci.fi * http://www.uta.fi/~f1sale
PATH=/bin:/usr/bin:/usr/local/bin
PID=$$
if [ $# -lt 3 ]; then
echo 1>&2 Usage: $0 old-string new-strings file [files]
exit 127
fi
OLD=$1; shift;
NEW=$1; shift;
BASENAME=`basename $1`
while [ $# -gt 0 ]; do
echo Changing $OLD to $NEW in $1...
sed "s/$OLD/$NEW/g" $1 > /tmp/$BASENAME.$PID
cp -f /tmp/$BASENAME.$PID $1
rm -f /tmp/$BASENAME.$PID
shift
done;
---------- snip --------------
HTH, P. *8^)
Reply to: