APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Unix Articles > Awk vs. Perl
Printer Friendly Version




Awk vs. Perl



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 answwered 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?


If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


6 comments




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





Mon Nov 9 17:01:23 2009:   AndrewSmallshaw

gravatar
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:   jtimberman

gravatar
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:   TonyLawrence

gravatar
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:   aMoLk

gravatar
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:   TonyLawrence

gravatar
Yes, that is a big advantage.



Wed Feb 29 23:38:28 2012:   Polaris

gravatar
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

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - AWK
       - Linux
       - MacOSX
       - Perl
       - Scripting
       - Shell




Unix/Linux Consultants

Skills Tests

Guest Post Here