Replace A Word From A Group of Files
Thursday, March 23rd, 2006It’s another time to help my friend at office. He started a website and put some google AdSense. I guess it doesn’t make any sense and not as easy as to make some bucks out of it.
Well, the Q0: Hey how could I replace a particular word with another in a file?
Open the file in vi editor, go to the command mode [esc :] and type %s/oldW/newW/g
This search for every occurrence of oldW then replaces it with newW globally.
Q1: Good, I gotta 100+ files under a directory and wanna replace a particular word with another which occurs in some files?
That’s the spirit!
#! /bin/bash
cd /go/to/the/directory/
for file in * # here we can specify *.html or *.txt
do
# if foobar exists, replace it with fubar, make a temp file
# and back to old file
sed ’s/foobar/fubar/’ $file > sedtemp
mv sedtemp $file
done
tail: he needs to replace the AdSence words from the html files