Skip to main content
added 5 characters in body
Source Link
ilkkachu
  • 148k
  • 16
  • 268
  • 441

Here is a "intuitive" answer, for a more in depth explanation of awk's mechanism see either @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next time the same value in the array is accessed, it will be positive and the whole condition will fail.

Here is a "intuitive" answer, for a more in depth explanation of awk's mechanism see either @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

Here is a "intuitive" answer, for a more in depth explanation of awk's mechanism see either @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next time the same value in the array is accessed, it will be positive and the whole condition will fail.

As my answer got downvoted (witout explanation) I decided to erase it ("It's their loss, not mine"), so, any reference to it isn't helpful anymore.
Source Link
user232326
user232326

Here is a "intuitive" answer, for a more in depth explanation of awk's mecanismmechanism see either @Isaac's or @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

Here is a "intuitive" answer, for a more in depth explanation of awk's mecanism see either @Isaac's or @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

Here is a "intuitive" answer, for a more in depth explanation of awk's mechanism see either @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

actually explain what values `a[$0]` gets, which operations are conditional on it, and which are not, etc.
Source Link
ilkkachu
  • 148k
  • 16
  • 268
  • 441

Here is a "intuitive" answer, for a more in depth explanation of awk's mecanism see either @Isaac's or @Cuonglm's

Let's seeIn this case,

 !a[$0]++ 

first !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

 a[$0] 

we look atuses the value ofcurrent line a[$0]$0 (arrayas key to the array a with whole input line, taking the value stored there. If this particular key was never referenced before, ($0a[$0]) as key) evaluates to the empty string.

 !a[$0] 

IfThe a[$0]! wasn't referencednegates the value from before,. If it is assigned thewas empty string. It will evaluate to either ""or zero (empty stringfalse) or 0 depending on context, we now have a true result.   If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that !a[$0] was not set to begin with, the whole line is negationprinted as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in testthe array is accessed, it will evaluate to true)be positive and the whole condition will fail.

  • we print the input line $0 (default action).

  • Also, we add one ( ++ ) to a[$0], so next time !a[$0] will evaluate to false.

Here is a "intuitive" answer, for a more in depth explanation of awk's mecanism see either @Isaac's or @Cuonglm's

Let's see,

 !a[$0]++ 

first

 a[$0] 

we look at the value of a[$0] (array a with whole input line ($0) as key).

 !a[$0] 

If a[$0] wasn't referenced before, it is assigned the empty string. It will evaluate to either "" (empty string) or 0 depending on context.  ( ! is negation in test will evaluate to true)

  • we print the input line $0 (default action).

  • Also, we add one ( ++ ) to a[$0], so next time !a[$0] will evaluate to false.

Here is a "intuitive" answer, for a more in depth explanation of awk's mecanism see either @Isaac's or @Cuonglm's

In this case, !a[$0]++, the post-increment ++ can be set aside for a moment, it does not change the value of the expression. So, look at only !a[$0]. Here:

a[$0] 

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!a[$0] 

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

added 2 characters in body
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107
Loading
correction as suggested
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107
Loading
Mod Moved Comments To Chat
Rollback to Revision 4
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
The value of a[$0] always exists, it exists as soon as it is accesed. What it could be is empty (null).
Source Link
user232326
user232326
Loading
“Then we add one” implied that the addition happened after printing, which is wrong. Reword so that the explanation is correct.
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
edited body
Source Link
slm
  • 380k
  • 127
  • 793
  • 897
Loading
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107
Loading