Is there a tighter (less characters) way to use perl on the command line to search and replace text from STDIN than I've got here? The code below works.

echo hi | perl -e '$a = <STDIN>; $a =~ s/i/o/g; print $a;'

1 Answer 1


You can use -p option (see perl --help):

assume loop like -n but print line also, like sed

So the script becomes:

echo hi | perl -pe 's/i/o/g'

which is really compiled to:

$ perl -MO=Deparse -pe 's/i/o/g'
LINE: while (defined($_ = <ARGV>)) {
continue {
    die "-p destination: $!\n" unless print $_;
-e syntax OK

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