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;'
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.
Sign up to join this communityYou 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