delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/03/17:10:59

Message-ID: <8D53104ECD0CD211AF4000A0C9D60AE301545D82@probe-2.acclaim-euro.net>
From: Shawn Hargreaves <ShawnH AT Probe DOT co DOT uk>
To: djgpp AT delorie DOT com
Subject: Re: Global file edit?
Date: Tue, 3 Aug 1999 09:50:09 +0100
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.0.1460.8)
Reply-To: djgpp AT delorie DOT com

Dave Scott writes:
> Is there a simple utility available to do something like this :-
>
>   edit <existing_string> <replacement_string> <a_bunch_of_filespecs>

A decent programming editor should have builtin support for 
search/replace across multiple files. But if you need to do this
in a standalone form, try something like:

---- cut here, globalreplace.sh ----

#! /bin/sh

if [ $# -lt 2 ]; then
   echo "Usage: globalreplace oldstring newstring files" 1>&2
   exit 1
fi

oldstring=$1; shift
newstring=$1; shift

echo "Changing $oldstring to $newstring"

for file in $*; do
	if [ -f $file ]; then
		echo "Editing $file"
		mv $file tmpfile
		sed -e "s/$oldstring/$newstring/g" tmpfile > $file
		touch -r tmpfile $file
		rm tmpfile
	else
		echo "Error: $file not found"
	fi
done



	Shawn Hargreaves.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019