perl -ne "/search/ and s/\n/foo\n/g and print" input.txt