Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What bash command can be used for regex substitution on a pipe?

     cat foo.txt | someprogram
share|improve this question
up vote 11 down vote accepted

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

share|improve this answer
sed isn't just a program, it's a way of life. – Joseph Kern Jun 15 '09 at 11:24

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
share|improve this answer
I recently discovered perl -pe, and it's great to have a much more powerful regex engine available :) – Rory Jan 22 '10 at 15:10

The program you are looking for is sed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.