3

Code is being used to remove duplicate entries from histroy.txt file contains history of command line.

BEGIN{
      if (data[$0]++ == 0)
         lines[++count] = $0;
     }
END {
     for(i=1; i<count; i++)
         print lines[i];
    }

What is data in the code and why it is being compared to 0?

2
  • 3
    Are you sure the data & lines code is in the BEGIN block? I'm not sure what $0 would be inside a BEGIN block; they'd make more sense inside the main body. – Jeff Schaller Nov 16 '17 at 15:30
  • oups didn't see the BEGIN ... – Archemar Nov 16 '17 at 15:35
5

This intent to remember uniq line in input.

as Jeff Schaller pointed out, $0 is undefined in BEGIN block.

a more correct code should be

{
      if (data[$0]++ == 0)
         lines[++count] = $0;
     }
END {
     for(i=1; i<count; i++)
         print lines[i];
    }

or even

!data[$0]++ { lines[++count] = $0; }
END {
     for(i=1; i<count; i++)
         print lines[i];
    }

The first time a line appear data[$0] will be equal to 0 and line[ ] will receive the line.

After test, data[$0] will be incermented (++ is a post incrementation) and test will evaluate to false for line with same content.

The END statement print all the line in order.

see also How does awk '!a[$0]++' work?

1
  • Does that code have any advantage over just !a[$0]++? – Wildcard Aug 21 '18 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.