Main Content

Awk v Perl

Archive - Originally posted on "The Horse's Mouth" - 2011-09-18 07:52:10 - Graham Ellis

"The AWK utility is a data extraction and reporting tool" - Wikipedia

"Perl was developed as a language to make report processing easier" - Wikipedia

So what's the difference?

Looking through all the lines of a file, report the 6th and 1st fields on each line that contains the string "pix" and report complete lines that contain the string "chamber":

In Awk:

  awk '/pix/{print ,};/chamber/{print}' extra_log

Read more about awk [here] on our website or find a full manual [here] on the Gnu site.

In Perl:

  perl -na -e '/pix/ and print "$F[5] $F[0]\n"; /chamber/ and print' extra_log

Find out how to learn more about Perl [here] on our web site. Full Perl documentation can be found [here] on the official Perl web site.

The "trick" is that awk is a useful data extraction and reporting utility, but that Perl grew eclectically and is now so much more. The blogging software that I use to manage these articles is written in Perl, and I can't even imagine trying to do that in Awk. You can write a short utility in awk even shorter than in Perl, but Perl's extensibiity through subs, modules, and much much more means that it't there for the long haul of data analysis long after you've stretched the limits of awk.

Mind you ... if I have the off "one-liner" to write in haste to filter specific data from a log file, I'll probably use awk!