O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18.1. Converting awk Programs to Perl

One of the many cool things about Perl is that it is (at least) a semantic superset of awk. In practical terms, this means if you can do something in awk, you can also do it somehow in Perl. However, Perl isn't syntactically compatible with awk. For example, awk's NR (input record number) variable is represented as $. in Perl.

If you have an existing awk program, and wish it to run with Perl, you can perform a mechanical translation using the a2p utility provided with the Perl distribution. This utility converts the awk syntax into the Perl syntax, and for the vast majority of awk programs, provides a directly runnable Perl script.

To use the a2p utility, put your awk program into a separate file and invoke a2p with the name of the file as its argument, or redirect the standard input of a2p to the file. The resulting standard output will be a valid Perl program. For example:

$ cat myawkprog
BEGIN { sum = 0 }
/llama/ { sum += $2 }
END { print "The llama count is " sum }
$ a2p <myawkprog >myperlprog
$ perl myperlprog somefile
The llama count is 15
$

You can also feed the standard output of a2p directly into Perl, because the Perl interpreter accepts a program on standard input if so instructed:

$ a2p <myawkprog | perl - somefile
The llama count is 15
$

An awk script converted to Perl will generally perform the identical function, often with an increase in speed, and certainly without any of awk's built-in limits on line lengths or parameter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required