×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

One of the most frustrating AWK programs

One of the most frustrating AWK programs

One of the most frustrating AWK programs

(OP)
Recently I wrote the following fragment in AWK by accident:

CODE --> AWK

A = "a"
B = "b"

{
}

END {
  print A, B
} 

And the result completely blowed my mind up.
Just try to run this code against e.g. the following input file:

CODE --> TXT

1
2
3 
and enjoy the result.

The AWK code above was actually a part of a bigger program - and, believe me, I spent an hour (!) in frustration, trying to understand what the heck was happening.

The issue with this code is that in fact it does absolutely, completely and fundamentally different things than you may expect from the code that looks like this.
Just compare it with the following, a little bit modified, code:

CODE --> AWK

BEGIN {
  A = "a"
  B = "b"
}

{
}

END {
  print A, B
} 

Looks pretty similar to the first code fragment, isn't it?
But the behavior is absolutely, completely and fundamentally different!

And here is what I am thinking about this:
- why the first code fragment is silently compiled by AWK without any warning???
I mean, what is the real use-case of the code like that? Would you really think that someone, who is in his own mind, would intentionally write the code like that, assuming the behavior that AWK provides for that? I would only think that someone could write such code intentionally with the only purpose of frustration and madness of everyone else who would use that code.
What do you think?
In my opinion, this question is worth to be raised to GAWK team with regards to provide at least a warning for such code.

RE: One of the most frustrating AWK programs

There is nothing wrong with the script below, save that it is an example of obfuscated awk scripting.

CODE --> awk

A = "a"
B = "b"

{
}

END {
  print A, B
} 
Moreover, it is semantically different from the script that you've posted although it looks syntactically similar to the one you're talking about.

RE: One of the most frustrating AWK programs

(OP)
I do understand that is a valid AWK program, but I tried to highlight the fact that the actual meaning of

CODE --> AWK

A = "a" 

according to AWK's interpretation, is

CODE --> AWK

(A = "a") != 0 { print $0 } 

and it is so unlogical that it is almost meaningless.

I mean, is there any sense to write such a condition that is always true and silently invokes "print $0"?
Personally I do not see any sense in writing this intentionally. In my opinion, something like this can be written only by mistake.

I would understand if it was e.g.

CODE --> AWK

A == "a" 

In such case it would be an obvious condition, that intentionally checks a variable A against the value of "a", with the default action of "print $0" assumed.
But what is the sense to do the same in case of assignment? Assignment, Carl!!!

RE: One of the most frustrating AWK programs

Your frustration is valid, but that is the purpose of writing obfuscated programs. Anyways I think we have beaten this dead animal far too much. It'd have been nice to put the comparison operator instead of the assignment one but since the latter evaluates to true for each line, they're all printed.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close