3
\$\begingroup\$

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;'
\$\endgroup\$
9
\$\begingroup\$

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>)) {
    s/i/o/g;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
| improve this answer | |
\$\endgroup\$

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