Perl's one-liner is based on the functions of awk's one-liner

Perl's one-liner is based on the features of awk's one-liner. awk is a row-oriented tool that defaults to processing each row. Perl programming isn't line-oriented, but you can use Perl's command-line options to represent awk's line-oriented one-liner.

To try the one-liner, prepare a multi-line file "text.txt".

abcd
ppp
abcppp

Perl's one-liner representation

A one-liner representation of Perl. I am printing a line that starts with abc. Each line is assigned to the default argument "$_".

cat text.txt | perl -n -e'if ($_ = ~ / ^ abc /) {print $_}'

-e is e for execute. You can execute the Perl program given by the argument. -n is n for line. Like awk, it's row-oriented and can be processed.

You can omit the default argument and write it a little shorter.

cat text.txt | perl -n -e'if (/ ^ abc /) {print}'

Awk's one-liner expression

It is a one-liner expression of awk. The line beginning with abc is output. In Perl, where "$_" is, it is "$0".

cat text.txt | awk'{if ($0 ~ / ^ abc /) {print $0}}'

You can omit the "$0" to make it a little shorter.

cat text.txt | awk'{if (/ ^ abc /) {print}}'

Curiously, it's exactly the same as Perl, except for the command line options and "{}".

Perl expects Unix / Linux users to handle complex things in stages

Perl is a general-purpose programming language, but when non-programmers are working with tools such as grep, sed, and awk, they need a general-purpose program when it goes beyond a certain complexity. I am assuming that.

Perl provides a step-by-step path for users of Unix / Linux tools to grow their system from Linux / Unix commands to naturally generic programming when they feel that way. ..

Perl exists as a tool in the Unix / Linux middleware toolbox, ready to be used by users when they need it.

Associated Information