LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-29-2008, 09:44 AM   #1
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Some comments on awk and awk scripts


Hi.
Quote:
Originally Posted by pppaaarrrkkk View Post
Thanks for replies, helpful all.

Shame AWK can't do it.
Conclusion is to use Python (or Perl), but that's another thread.
This is the other thread

From my perspective, awk cannot deal with objects smaller than characters, so it does not do well if you need to process binary (non-text) files. However, as seen, it can be used to solve the problem cited, just not as compactly as one often sees for awk solutions.

As ghostdog74 has mentioned here or in other forums, awk is often faster than alternatives, such as perl and sed -- at least consuming less CPU time, I don't recall about real time, system time, or other resources, memory, etc.

For one-shot solutions, I often reach for awk. However, if I see that I can attack a class of problems with a little generalization, then I will usually craft a perl solution, often because of the large body of modules contributed by the perl community. These are often available with one's package manager, or, in the last resort, from http://cpan.org/

I also usually write in a line-oriented sense. So in the thread above, one solution was, essentially:
Code:
{$1=$1}1
The assignment was explained as reassembling the line. However, the trailing "1" might be mystifying. I would write it as:
Code:
    { $1 = $1 }
1
because it really is two statements. Recall that the format for awk is a series of lines like:
Code:
expression  { action }
if expression is missing, it is taken to mean "always true" or "for every line".

If { action } is missing, it is taken to be
Code:
print $0
So the "1" really means
Code:
for every line  { print whatever is in $0 }
because "1" is "true".

If I don't write it out or comment things like that, then in a month or so I'll forget what it means, and I'll need to spend time re-discovering what I was doing.

Best wishes ... cheers, makyo
 
Old 02-29-2008, 10:51 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by makyo View Post
[...]
If I don't write it out or comment things like that, then in a month or so I'll forget what it means, and I'll need to spend time re-discovering what I was doing.
I disagree,
this will not happen if you _know_ Awk and its idioms.
 
Old 02-29-2008, 06:35 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,086

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by radoulov View Post
I disagree,
this will not happen if you _know_ Awk and its idioms.
Hmmm.... it seems that the OP _knows_ Awk, but even then it is very hard to _remember_ the hundreds of smart Awk constructs. The same goes for Bash, same smart constructs are possible, but it takes some time to realize what the intended action was if you read the code some months later. Let alone someone else understands it.

If you speak C, and you think you speak it well, google for "obfuscated C code" and see if you understand anything.

It is always a bad idea to write code which is less than optimal transparent you can get. Even if you know the language. One day you get punished for that.

jlinkels
 
Old 02-29-2008, 09:16 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
but then again, same goes to Perl (or others), which has even more such "idioms".
Anyway, I think what makyo meant if i am not wrong, is that we should comment our code or write them legibly, when using such "idioms".
 
Old 03-02-2008, 05:39 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,264

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
concur with jlinkels & ghostdog74
 
  


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
Awk opensource82 Linux - General 5 09-19-2007 09:08 AM
Help with awk kshkid Programming 1 12-22-2006 05:59 AM
Value counting in awk scripts sarajevo Programming 9 10-20-2006 08:24 AM
Help needed in writing Awk Scripts.. TheDarktrooper Red Hat 1 05-04-2004 06:49 PM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 06:37 PM


All times are GMT -5. The time now is 02:36 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