5

In other programming languages there often has a bool or boolean type. I can create a boolean type variable and use the not operator to toggle it. I can toggle it many times and get a series of true, false, true, false ...

In awk there is no boolean type. Only empty string and the number 0 are considered false. All other values are true. In this case what should I do if I want to do operation A on line 1, 3, 5... and operation B on line 2, 4, 6... The task will very easy if we have a boolean type variable and I can simply not it.

Just a learner
  • 1,766
  • 4
  • 22
  • 32
  • 1
    not sure if bool types are there, but you can use [bitwise operations](https://www.gnu.org/software/gawk/manual/html_node/Bitwise-Functions.html).. for example: `/condition/{a = xor(a,1)}` – Sundeep Sep 08 '16 at 08:46
  • 1
    Does modulo division help? `NR%2 {do A} !(NR%2) {do B}` – steeldriver Sep 08 '16 at 08:48

1 Answers1

8

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

basically use a=!a this will negate a turning 0 to 1 and 1 to 0.

you can test with

ls | awk '{a=!a; if ( a ) printf "good %s\n",$0 ; else printf "bad\n";}'
Archemar
  • 31,183
  • 18
  • 69
  • 104