My preferences are in this order: perl, vim (vi, gvim, etc.) substitution commands, sed, and awk.
perl is a heavy software program that can do math as well. See http://perlmonks.org/ for more details.
Vi is the well known text editor all *ix systems have (Linux, Unix, etc.), which, incidentally, can do some nice scripting.
Sed is, well see http://linux.die.net/man/1/sed
Awk (gawk and its brethren) is, well see http://linux.die.net/man/1/awk
... almost forgot. The absolute best, and with a price tag to match, is PowerGrep. Without a doubt, the easiest way to formulate complex queries and replacements of text in DOC, TXT, and other files.