LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-18-2007, 09:31 AM   #1
jayeola
Member
 
Registered: Dec 2005
Location: london, uk
Distribution: BLAG GNU/Linux
Posts: 58

Rep: Reputation: 15
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?
 
Old 12-18-2007, 09:43 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,436

Rep: Reputation: 83
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!
 
Old 12-18-2007, 11:09 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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
 
Old 12-18-2007, 11:27 AM   #4
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
Old 12-18-2007, 01:57 PM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,061

Rep: Reputation: 769Reputation: 769Reputation: 769Reputation: 769Reputation: 769Reputation: 769Reputation: 769
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...
 
Old 12-18-2007, 05:47 PM   #6
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
Old 12-18-2007, 08:14 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by PAix View Post
Windows - just don't go there with awk, you will end up dissapointed.
This should work ok on Windows.
 
Old 12-18-2007, 08:26 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by ghostdog74 View Post
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
 
Old 12-18-2007, 08:51 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Tinkster View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
how can I invoke "awk" from shell to do floating point math? Joseph Schiller Programming 8 01-12-2006 05:00 AM
"mawk" dependency missing in CD install lilsirecho Arch 8 10-26-2003 11:14 AM
awk/gawk handling of "delete" ? realos Programming 1 06-26-2003 09:45 AM


All times are GMT -5. The time now is 06:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration