Bash command for regular expression substitution

Solution 1:

You probably want

sed 's/exp1/exp2/g' foo.txt > foo2.txt

Read more at Sed tutorial, Another tutorial, and A small tutorial at Linux HOWTOs

Solution 2:

You can also use perl one liners if you find you want more regular expression features than sed provides. See this link for a comparison. nik's example would look like:

perl -ple 's/exp1/exp2/g' foo > foo2.txt

Solution 3:

The program you are looking for is sed.