I would like to get help to find how to replace word in files from command line instead of to vi to each file.
This is the command i am running now.
grep <old word> *
vi (file with the word found in it)
1,$s/<old word>/<new word>/g
It would very helpful if I can combine these in one commad line.
for FILE in file1 file2 file3
do
sed 's/oldword/newword/g' < "$FILE" > /tmp/$$
cat /tmp/$$ > "$FILE".new
done
rm -f /tmp/$$
Remove the .new only once you're sure it does what you want. Overwriting your originals with bad data can be a hard mistake to recover from...
If your sed supports --in-place then you could also try this:
OLD=$1
NEW=$2
for file in ./*
do
if [ -f "$file" ] && grep -q "$OLD" "$file"
then
echo "Updating $file (backup to ${file}.old)"
sed --in-place=.old "s/$OLD/$NEW/g" "$file"
fi
done