APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Awk vs. Perl

© November 2009 Anthony Lawrence

At any Linux/Unix forum a large number of posts have to do with sed and awk - how do I do this, how do I do that?

I can see using sed. It's quick, it's simple (unless we are doing something complicated) and easy to learn. On the other hand, we have "awk", which is nasty and error prone even in its most basic invocations.

That's just my opinion, of course.

Sure, I used to use awk. When I used it, you weren't likely to find Perl on most Unix systems, so for a lot of text mangling, awk was at least easier than writing in C or anything else. It did the job, and you'd get used to its quirks.

As Perl became ubiquitous, I thought of awk like something sticky you'd find under your car seat. Whatever use that thing may have had in the past, it's heading for the trash now. Why on earth would I suffer with awk's limitations and difficult synatx when I have Perl?

For me, awk synatx always seemed, well, awkward. Perl immediately made sense to me. In a way, that's scary, because it could mean that my mind works like Larry Wall's mind, and if that wouldn't cause you to check in with a psychiatrist, I don't know what would.

I'm hardly alone in that opinion. Many of those forum questions end up being answered by a Perl script. To me, the Perl solution always seems easier to read and understand (that may just be my screwy mind, again).

Yet the forum questions persist. Some of it is homework: you can often tell when the petitioner includes such damning information as "I have to use awk. Can't use Perl" or forgets to delete instructions like "Show your work" when cutting and pasting. But it's not all homework: apparently a large number of people still use awk.

I went looking for why and found this: AWK Programming . Some of the arguments made there in favor of awk include:

  • awk is simpler (especially important if deciding which to learn first)
  • awk syntax is far more regular (another advantage for the beginner, even without considering syntax-highlighting editors)
  • you may already know awk well enough for the task at hand
  • you may have only awk installed
  • awk can be smaller, thus much quicker to execute for small programs
  • awk variables don't have `$' in front of them :-)
  • clear perl code is better than unclear awk code; but NOTHING comes close to unclear perl code

I'm not entirely in agreement - I don't think awk is "simpler" or has better syntax, but I can see that you could argue that. But even that page admits that "Generally Perl might be better for really complex tasks". I'd argue that awk is clumsy at complex tasks, but I definitely have to agree with that last comment about "unclear" code.

Probably many of the readers here used awk before discovering Perl. Did Perl win you over or do you still find yourself typing "awk" frequently? If you haven't used Perl, is it the learning curve? Or just that you can do whatever you need with the tools you already have?

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Awk vs. Perl


Inexpensive and informative Apple related e-books:

Take Control of iCloud

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of IOS 11

Take Control of OS X Server

Take Control of Pages

More Articles by © Anthony Lawrence

Mon Nov 9 17:01:23 2009: 7505   AndrewSmallshaw

I've always thought the greatest strength of awk is its convenience - it will be installed on a Unix system and it has enough in common with C for there to be at least some carry-over. Too often tool selection is not about niceties such as how elegant the code is - you have some kind of task to sort out and it needs doing NOW. Learning a new tool is often not for future reference, but to carry out an immediate task.

The best way to learn a new tool is to spending a couple of days (or even an afternoon) playing around with it when no immediate deliverables are expected. If that is not the case known tools (or something that looks like it can be picked up quickly) are more attractive, even when there may be clearly more appropriate alternatives. That's why I first picked up awk, and I think I was right - my first (100 line) script took around four hours - a long time, but comparatively short for learning a new tool. What I couldn't justify for that job was going off and spending a couple of days learning perl. It doesn't matter how suitable it would be, or how it pays off over the long run - sometimes the immediate need is more pressing.

Of course, once you know one tool for a job it becomes more difficult to justify learning a second tool for the same class of problems. You might like to learn that alternative but face it - there are too many things out there and so new learning generally has to be prioritised according what is most needed.

Mon Nov 9 17:22:38 2009: 7507   jtimberman

I learned awk and sed at first, and I still use them for two specific reasons:

sed 's/something/somethingelse/'
awk '{print $2}'

I used awk for some more complex functionality due to a work project requirement. I tried Perl, and used it for a few projects, but it just never clicked with me, especially all the quirky variable sigils and their usage.

Fast forward to now, I use Ruby for everything. It makes more sense to me than Perl ever did.

Mon Nov 9 21:43:20 2009: 7508   TonyLawrence

That's so interesting. For me, Ruby is confusing - Perl makes sense. I was using awk regularly but when I leaned Perl, I dropped awk like worn out sneakers.

Odd how our minds can work so differently.

Sun Aug 8 19:50:04 2010: 8879   aMoLk


I have been using awk and sed for few years managing unix systems. I mostly write scripts in korn shell, and while my scripts may range from small to upto a few hundred lines, the requirement of awk is usually pretty limited to 5-10 lines max. So, for me awk works since it is present on 100% of the systems that I work on, while perl may not be installed in some cases. So, awk remains the choice for me. Although, I've been trying to learn perl lately, so that I can use both tools on a as needed basis.
My inclination towards perl is simply because I can use it on windows as well.

Sun Aug 8 19:57:57 2010: 8880   TonyLawrence


Yes, that is a big advantage.

Wed Feb 29 23:38:28 2012: 10676   Polaris


awk isubiquitous. it feels much more shell like like than perl. Also, it's rather C like. These are my opinions, butthey make awk seem very friendly to me.

I like using awk to do "super-scripting" I define these as tasks that are a bit ambitious for bash but not big enough for a major project. Of course, the downside is that now I use awk for bigger things than it should be...

also, i like awk better the sed for "one off" filtering. I think it's a bit saner to the reader during the tweaking process.

I wish I knew perl better, but so far I've only dabbled. I do really like awk, though

Wed Aug 15 21:12:48 2012: 11237   Michael


awk is the first choice in two cases:
awk 'condition {print $2}'
where condition can be null or /EREsearch/ or $1=="string" or $1~/EREsearch/ or $1>45 or ...
with associative array. Example:
awk 's[$0]++==0'
is short for
awk 's[$0]++==0 {print}'
Effectively it omits duplicate lines without changing the order.
Compare with an expensive sort | uniq or sort -u !
The associative arrays in Perl are more cryptic.

Wed Aug 15 21:37:55 2012: 11238   TonyLawrence


Cryptic? Different stores, I guess. I find awk cryptic - Perl makes sense to me!


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Unlike info, pinfo does not display anything if it has nothing. I've been forever irritated by info coming up with its default page when it has nothing to tell me. (Tony Lawrence)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode