Skip to main content
added 264 characters in body
Source Link
jot 12 | awk '(mon_ = +$_+$1) % 2 -!= (7 < mon_)'
                                        or                   
         awk '((_ = +$1) + (7 < _)) % 2'
     1
     3
     5
     7
     8
    10
    12

Conversely, checking for a month being short would be :

jot 12 | awk  '(_ = +$1) % 2 == (7 < _)'
         awk  '(_ = +$1) % 2 -  (_ < 8)'                               
         awk '((_ = +$1)     +  (_ < 8)) % 2'

     2
     4
     6
     9
    11
jot 12 | awk '(mon = +$_) % 2 - (7 < mon)'
     1
     3
     5
     7
     8
    10
    12
jot 12 | awk '(_ = +$1) % 2 != (7 < _)'
                                        or                   
         awk '((_ = +$1) + (7 < _)) % 2'
     1
     3
     5
     7
     8
    10
    12

Conversely, checking for a month being short would be :

jot 12 | awk  '(_ = +$1) % 2 == (7 < _)'
         awk  '(_ = +$1) % 2 -  (_ < 8)'                               
         awk '((_ = +$1)     +  (_ < 8)) % 2'

     2
     4
     6
     9
    11
added 264 characters in body
Source Link

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

A real world example of this particular combination would be for checking whether a certain month has 31 days or not :

jot 12 | awk '(mon = +$_) % 2 - (7 < mon)'

     1
     3
     5
     7
     8
    10
    12

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE. Its twin via logical comparison operators would be :

awk '/regexp9/ >= /regexp10/'

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE. Its twin via logical comparison operators would be :

awk '/regexp9/ >= /regexp10/'

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

A real world example of this particular combination would be for checking whether a certain month has 31 days or not :

jot 12 | awk '(mon = +$_) % 2 - (7 < mon)'

     1
     3
     5
     7
     8
    10
    12

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE. Its twin via logical comparison operators would be :

awk '/regexp9/ >= /regexp10/'

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

added 92 characters in body
Source Link

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE. Its twin via logical comparison operators would be :

awk '/regexp9/ >= /regexp10/'

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE.

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

just directly MULTIPLY the patterns if you want them all to be true, thus eliminating any and all conditional branching

awk '/regexp1/ * /regexp2/ * /regexp3/ … '

say if you need regex 4 FALSE while regex 5/6 both being TRUE, then you can lump them all into a single compare :

awk '/regexp4/ < /regexp5/ * /regexp6/'

or say if you want to match either regex 7 or regex 8, but not both at the same time, then do either one of these

logical "!=" NOT EQUAL

awk '/regexp7/ != /regexp8/'

arithmetic "-" MINUS, since [ A XOR B ] on a single-bit level 
is same as checking for non-zero result of subtraction

awk '/regexp7/ - /regexp8/'

here's the strangest one of them all - if you want either regex 9 to be TRUE or regex 10 to be FALSE, and wanna do it without conditional branching :

awk '/regexp9/ ^ /regexp10/'

That's right - regex 9 RAISED TO THE POWER of regex 10. It works because

    1 1 1^1 ->  1
    1 0 1^0 ->  1
    0 1 0^1 -> [0]
    0 0 0^0 ->  1

So the only time this algebraic expressions yields FALSE would be when regex 9 is FALSE while regex 10 is TRUE. Its twin via logical comparison operators would be :

awk '/regexp9/ >= /regexp10/'

All these might not appear to be idiomatic, but they're all POSIX-compliant awk syntax that is fully portable.

added 490 characters in body
Source Link
Loading
Source Link
Loading