LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk, mawk, gawk - which is "better"? (https://www.linuxquestions.org/questions/programming-9/awk-mawk-gawk-which-is-better-607623/)

jayeola 12-18-2007 09:31 AM

awk, mawk, gawk - which is "better"?
 
I'm thinking that awk is most likely to be the most ubiquitous and hence is better to use. Am I right in thinking that it's better to use awk than any other flavours?

kaz2100 12-18-2007 09:43 AM

Hya,

It all depends what you want to do.

If only you use your code on your comp (penguin, apple, solaris, hp or whatever) it is your choice.
If you want to circulate your code, compatibility is another issue. (also true, when you have many hardwares.)

Happy Penguins!

makyo 12-18-2007 11:09 AM

Hi.

Yes, awk is the name that is generic. However, I am told that awk on Solaris is the verrry old awk, and that most knowledgeable people use nawk on that platform.

It is often said that mawk is faster than the others.

The gawk probably has the best documentation.

I'd probably stay with awk, and put a comment to use nawk on Solaris (in fact, that is what I do):
Code:

# Use nawk or /usr/xpg4/bin/awk on Solaris.
... cheers, makyo

PAix 12-18-2007 11:27 AM

History lesson I'm afraid, then you will be able to decide for yourself which is the more appropriate depending on circumstances.
Originally the language was called awk. In approx 1989 nawk (new awk) superseded awk and later gawk (Gnu awk) extended the langauge further. So simple, use gawk!

Not so fast. If you use Solaris then you will probably find that you are limited to what is installed by default, unless you have a very progressive sysadmin that has installed gawk, but probably not. Solaris normally has installed both awk and nawk. The original awk will give errors if you try to use floating point numbers and in retrospect is severely short of really useful features. So the answer on Solaris is to use nawk.

Now on other distros either because of symlinking or otherwise (I'm hedging my bets here because I'm moving away from my home turf) nawk or gawk are refered to as awk, but not universally so.

Windows - just don't go there with awk, you will end up dissapointed.

tronayne 12-18-2007 01:57 PM

An additional monkey wrench in the works.

The awk program is named after its authors, Alfred Aho, Peter Weinberger and Brian Kernighan all at the time of AT&T Bell Laboratories. The original awk dates from a long, long time ago (1977). New awk was released in conjunction with Aho et. al. The AWK Programming Language (Redding, MA: Addision-Wesley Publishing Co., 1988), ISBN 0-203-07981-X. New AWK, called nawk, is completely documented in this book. As to the differences, the preface, pp. v-vi briefly outlines:
Quote:

Evolution of the AWK Language
...
The major new feature is the ability for users to define their own functions. Other enhancements include dynamic regular expressions, with text substitution and pattern-matching functions: additional built-in functions and variables; some new operators and statements; input from multiple files; and access to command-line arguments. Error messages have also been improved..."
Because Solaris adheres pretty much to System V Release 4 (with Berkeley enhancements), it contains both the original AWK oawk and nawk. The gawk program is a GNU implementation of nawk (as I understand it) with additional enhancements, many of which are not compatible with nawk, and, gawk is what you get with Linux.

My own experience has been that going from Linux to Solaris can sometimes be a royal pain where you sit and I have learned (the hard way, as usual) to avoid GNU-specific enhancements to any language implementation, not just AWK. I have the source code for nawk and run it on my Linux boxes just to avoid those kind of problems and life is better.

Is there any "best?" Well, best is a relative term. In my book, best means no problems porting high-level software from one platform to another and that means avoiding enhancements because they will always come back and bite you in the butt (that was learned the hard way porting DEC FORTRAN programs -- chock full of DEC's enhancements -- to System V). If you find a copy of the book and adhere to the grammar and syntax therein you'll probably not have a problem with gawk on Linux and nawk on Solaris. Personally, on Solaris, I write in nawk grammar and syntax to take advantage of the authors' enhancements from oawk; and, you probably won't have gawk available on a Solaris box out of the carton (system administrators where I get paid are really leery of wily-nilly installing GNU utilities because of past compatibility problems that nobody really wants to spend time dealing with when an analog is sitting there to begin with).

For what it's worth...

PAix 12-18-2007 05:47 PM

It's worth a lot. I have the book - if I can find it - and believe me, it isn't really very thick and that can't be said for most languages. If you are sticking with nawk (1989), then it's exceedingly handy to have it to hand on the desk.

ghostdog74 12-18-2007 08:14 PM

Quote:

Originally Posted by PAix (Post 2994841)
Windows - just don't go there with awk, you will end up dissapointed.

This should work ok on Windows.

Tinkster 12-18-2007 08:26 PM

Quote:

Originally Posted by ghostdog74 (Post 2995294)
This should work ok on Windows.

It does. But it doesn't compare too well to the same tool
on the same hardware in the better OS. Performance-wise,
that is; the functionality is obviously the same.

I've done a bit of testing about a year ago, and the run-times,
specially for larger files, sucked real badly in comparison.



Cheers,
Tink

ghostdog74 12-18-2007 08:51 PM

Quote:

Originally Posted by Tinkster (Post 2995303)
It does. But it doesn't compare too well to the same tool
on the same hardware in the better OS. Performance-wise,
that is; the functionality is obviously the same.

I've done a bit of testing about a year ago, and the run-times,
specially for larger files, sucked real badly in comparison.


Cheers,
Tink

that's good to know. thanks.
hope one year from that time, things have been done to address this.
How about compiling the source on windows?


All times are GMT -5. The time now is 05:50 PM.