On 26/05/2006, at 6:50 PM, Robert Thorsby wrote:
On 2006.05.26 18:37 john hedge wrote:
I've been hitting my head all day over awk and
sed (grep?)!
My challenge is to search a directory of text files
for a string which I want to replace wherever it
occurs with a new string and save the file with
the changes in the same original file name.
Try a loop using grep with the -q option to locate the files with
the requisite text then sed with the -i option to make the change.
Something like:
cd /path/to/directory/
for FILE in
do
if grep -q "text to be replaced" "$FILE"
then
sed -i 's/text to be replaced/replacement text/g' "$FILE"
fi
done
Notes:
1. "grep -q" does not need to be in [test brackets] because it is a
test in its own right
2. if you have filenames with spaces or funny characters it serves
yourself right.
3. In case you want this to be portable, keep in mind that '-i' is a
nonstandard extension. '-i' takes an argument, on my platform at least.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html