in reply to Re: AWK and Perl on UNIX
in thread AWK and Perl on UNIX

I still find that, after all these years, it's easier to write

ls -l|awk '{print $5}'

than

ls -l|perl -alne 'print $F[4]' or Newton forbid... ls -l|perl -ane 'print "$F[4]\n"'

Not only is the awk solution clearer, it also has a lighter resource footprint.

That is, when you string together long pipes on the command line, awk is often more compact than Perl. That's about all I use awk for these days, but I sure used to sling around a lot of data with it in the 80s.

It's not difficult to learn to write baby awk, and if you do it will serve you well. If the job gets too tough, though (think: you can no longer run it from the command line) then it's time to switch to Perl.

For example, user functions were retrofitted into the language, and the syntax is a bit of a botch. I can't even remember what it was now, but there was a gotcha that would rear its head up every so often and bite you.

Just because you have a chainsaw doesn't mean you can't use a pair of secateurs.



--
g r i n d e r

Replies are listed 'Best First'.
Re: awk has its place
by davorg (Chancellor) on Mar 30, 2001 at 19:38 UTC

    I'd do that as:

    ls -l|perl -pale '$_=$F[4]'

    which saves a couple of characters. I know what you mean, but I forgot most of the awk and sed that I ever knew when I learnt Perl.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      True enough, but using fewer characters isn't necessarily faster (to type). There's four switches needed, whereas that p-r-i-n-t just rolls off the finger r-l-r-r-l and that tick dollar underscore equals dollar F has you bouncing up and down on the shift key. It just ain't as smooth.

      Reflecting on this, the few vestiges of awk that remain are more in my fingers than in my head.


      --
      g r i n d e r