Don't forget about the everfaithful -i.bak
command line flag. It's one of my favourites. It edits a file "inplace" one line at a time. This should delete everything that doesn't contain the string "foo" (but I haven't tested it. sorry)
perl -epi.bak "print if(m/foo/);" foo.txt bar.txt baz.txt
Read on for the correct answer. I really should have tested it. Thanks Mirod and Btrott. ++ to both of you. -- for me.